Making a Health Pickup

This is an easy tutorial This is an easy tutorial. < Making an Explosion Course Making a 2D Platformer > This tutorial is part of a series on scripting. Return to tutorial index

In the last tutorial we covered how to make an explosion-filled gauntlet. In this tutorial we will cover handling collisions and player health to make health packs a player can walk over to get healed.

To start we will add a health pack into our game. This health pack will have a script inside of it.

MakeAPickup Healthpack.png

MakeAPickup HealthpackExplorer.png

For simplicity this tutorial is using a Union made with SolidModeling for the health pack. If you are using a model instead with several parts, put the script in the biggest part of your model.

Touch Event[edit]

In Lighting Parts on Fire we used a Touched event to trigger either creating or removing fire from a part. In this case, we want any player that touches the health pack to get healed. To start with though, we need to setup the Touched event. In this touched event we need to check if the thing that touched the health pack was actually a player character. Otherwise, the healthpack would try to heal anything it came in contact with! To check this, we will see if the part that touched the health pack is in the same model as a Humanoid, a special type of Instance that is needed in player characters and NPCs. Just like in Lighting Parts on Fire, we will use FindFirstChild to check if a Humanoid is present.

local healthPack = script.Parent
 
local function handleTouch(otherPart)
	local character = otherPart.Parent
	local humanoid = character:FindFirstChild('Humanoid')
	if humanoid then
		print('We should heal!')
	end
end
 
healthPack.Touched:connect(handleTouch)

Now let's add code to heal the player. First we will create a variable and in it store the amount we want to heal. By default ROBLOX characters have 100 health, so let's set the healing amount to 30. We can then use the Health property of the Humanoid we found to add health to the player that touched the healthpack.

local healthPack = script.Parent
local healAmount = 30 
local function handleTouch(otherPart)
	local character = otherPart.Parent
	local humanoid = character:FindFirstChild('Humanoid')
	if humanoid then
		local currentHealth = humanoid.Health		local newHealth = currentHealth + healAmount		humanoid.Health = newHealth	end
end
 
healthPack.Touched:connect(handleTouch)
Whenever your code needs a number, such as the amount of health a healthpack heals, the damage a rocket deals, or the duration of a speed boost, it is good practice to store that number in a variable like we did above. In the above code we certainly could have simply written:
local currentHealth = humanoid.Health
local newHealth = currentHealth + 30
humanoid.Health = newHealth

You should not do that! If we put that 30 into a variable at the top of the script, it is very easy to find and change if we decide it needs to be different later. Also, if we decide we need to use that value in multiple places in our script we can have wboth places use that variable. That way if we change the value in the variable, it changes for all places in code that we use it.

Testing the healthpack[edit]

Now the healthpack will heal characters who touch it. But how do we test this if our character always starts at full health? One solution is to change the health of the player in Studio after while the game is running. To do this, press the Play button and open the Explorer and Properties windows. In the explorer you will see a model called Player1. This is the character model for our current character. If you expand that model you will see its Humanoid. Click on the Humanoid in the Explorer and then find the Health property in the Properties window. We can then set this to whatever number we want. For testing purposes lets set it to 1.

MakeAPickup HumanoidExplorer.png

Remember, Player1 will only show up when you play the game! When you are just editing or if you press Run, it will not be in the Workspace.

Debounce[edit]

After changing your player's health to 1, notice how when we step on the health pack our health instantly refills. This is because the Touched event will keep firing while our character is in contact with the health pack. This means the function we wrote is being called over and over again really fast. We need to put some kind of delay in our healthpack so it can't be used again immediately. Creating a delay in this way is called debouncing, and is an important concept that you will use often when making games.

To setup our debounce, lets first make two new variables. The first variable will be a number representing how many seconds we want the healthpack's cooldown to last. Let's set it to a low number like 5 for testing purposes. The next variable will be a special kind of variable called a boolean. A boolean is a simple type that can only be one of two values: true and false. Booleans are particularly useful in if statements. We will name this new variable canHeal and set its value to true.

local cooldown = 5
local canHeal = true

Now we will use these variables to keep track of whether the healthpack can be used or not. In the if statement where we see if the humanoid exists, let's also check to see if canHeal is true. Inside the if statement we will then immediately set canHeal to false. This way if the function fires again while we are healing the if statement will see that canHeal is false and won't execute the code inside. After we heal the character, we can then wait for the duration of the cooldown variable we set and then set canHeal back to true.

local healthPack = script.Parent
local healAmount = 30
local cooldown = 5local canHeal = true 
local function handleTouch(otherPart)
	local character = otherPart.Parent
	local humanoid = character:FindFirstChild('Humanoid')
	if humanoid and canHeal then		canHeal = false		local currentHealth = humanoid.Health
		local newHealth = currentHealth + healAmount
		humanoid.Health = newHealth
		wait(cooldown)		canHeal = true	end
end
 
healthPack.Touched:connect(handleTouch)

Finishing Touches[edit]

Our healthpack is now functional but there are a few things that will make it a little better. Firstly, it should probably not heal characters who are already at full health. It should also probably not heal characters without health who are fully knocked out. We could check the touching player's health in the same line as when we check if humanoid and canHeal, but that would make the resulting line of code very long and unreadable. We can include another if inside our if statement and there check if the humanoid's health is between 0 and max. Don't forget to include another end to match the new if we're adding!

local healthPack = script.Parent
local healAmount = 30
local cooldown = 5
local canHeal = true
 
local function handleTouch(otherPart)
	local character = otherPart.Parent
	local humanoid = character:FindFirstChild('Humanoid')
	if humanoid and canHeal then
		if humanoid.Health > 0 and humanoid.Health < humanoid.MaxHealth then			canHeal = false
			local currentHealth = humanoid.Health
			local newHealth = currentHealth + healAmount
			humanoid.Health = newHealth
			wait(cooldown)
			canHeal = true
		end	end
end
 
healthPack.Touched:connect(handleTouch)

If would also be nice to have a visual indication when the healthpack can't be used. In this example, since the healthpack is in a single part, we can just adjust its transparency while the healthpack can't be used.

local healthPack = script.Parent
local healAmount = 30
local cooldown = 5
local canHeal = true
 
local function handleTouch(otherPart)
	local character = otherPart.Parent
	local humanoid = character:FindFirstChild('Humanoid')
	if humanoid and canHeal then
		if humanoid.Health > 0 and humanoid.Health < humanoid.MaxHealth then
			canHeal = false
			local currentHealth = humanoid.Health
			local newHealth = currentHealth + healAmount
			humanoid.Health = newHealth
			healthPack.Transparency = .5			wait(cooldown)
			healthPack.Transparency = 0			canHeal = true
		end
	end
end
 
healthPack.Touched:connect(handleTouch)

Now we have a healthpack that can heal players! Note that you can use the same concepts to create all sorts of effects (such as lighting players on fire, causing damage, creating an explosion, etc).

This is an easy tutorial This is an easy tutorial. < Making an Explosion Course Making a 2D Platformer > This tutorial is part of a series on scripting. Return to tutorial index