Throughout the Basic Coding courses, you have scripted individual parts to create playable scenes. With the previous method, if you were to duplicate the parts you would then also have duplicate scripts. This would make updating the scripts tedious as changes would have to be done script by script.
In this course, a different pattern will be used to create a number of health pickups, with only a single copy of the script which determines the health pickup behaviour. When the pickup is touched, it will restore the player’s health, fade slightly and be disabled for a short period of time.
Setting Up
First up, you’ll need a part or a model to use as a pickup. The Showdown Town example world includes plenty of health pickups spread all over the map - you can open it in Studio by clicking Edit as shown.
Each health pickup is a Union of two rectangular parts with a green PointLight inside. They’re all stored in one folder in the Workspace called HealthPickups, which is where the script will look for them. If you add any more to the map, it’s essential you ensure that they are also stored in this folder.
Restoring Health
To begin with, the script needs to restore a player’s health. This pattern should be familiar to you from the Deadly Lava course.
-
In ServerScriptService, add a script called PickupManager.
-
In this script, declare a constant called
MAX_HEALTH
with the value 100. -
Create a function called
onTouchHealthPickup
with parameters for the other part that touched the pickup and the pickup itself.local MAX_HEALTH = 100 local function onTouchHealthPickup(otherPart, healthPickup) end
-
In the function, get the character model from the parent of
otherPart
. Next, check to see if it has aHumanoid
usingInstance/FindFirstChildWhichIsA|FindFirstChildWhichIsA
. -
If it has a humanoid, set their Health property to
MAX_HEALTH
.local MAX_HEALTH = 100 local function onTouchHealthPickup(otherPart, healthPickup) local character = otherPart.Parent local humanoid = character:FindFirstChildWhichIsA("Humanoid") if humanoid then humanoid.Health = MAX_HEALTH end end
FindFirstChildWhichIsA
The code here calls FindFirstChildWhichIsA
– which takes the type of the object desired – instead of FindFirstChild
which only takes the name. This is a safer option as it can only ever return a Humanoid instead of something which just happens to be called “Humanoid”.
Getting the Pickups Folder
The folder holding the health pickups may not have loaded into the game by the time the script runs. WaitForChild
can be used to pause the script and get the HealthPickups folder when it loads.
When called on a folder, the GetChildren
function will return an array of the folder’s contents.
-
Beneath MAX_HEALTH, declare a variable called
healthPickupsFolder
and use theWaitForChild
function to get the HealthPickups folder from the Workspace. -
Create a variable named
healthPickups
to store the result of calling theGetChildren
function onhealthPickupsFolder
.local MAX_HEALTH = 100 local healthPickupsFolder = workspace:WaitForChild("HealthPickups") local healthPickups = healthPickupsFolder:GetChildren() local function onTouchHealthPickup(otherPart, healthPickup) local character = otherPart.Parent local humanoid = character:FindFirstChildWhichIsA("Humanoid") if humanoid then humanoid.Health = MAX_HEALTH end end
Next Page Connecting the Pickups