PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Health Pickups

Health Pickups

Oct 26 2018, 5:58 PM PST 10 min

In the tutorial Articles/Making an Explosion Course we covered how to make an explosion-filled gauntlet. In this tutorial, we will cover handling collisions and player health to create health packs that a player can walk over in order to heal themselves.

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

MakeAPickup_Healthpack.png

MakeAPickup_HealthpackExplorer.png

Touch Event

In Articles/Player-on-Part Touch Detection – 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)

Testing the healthpack

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 let’s 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

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 = 5
local 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

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).

Tags:
  • pickup
  • health
  • coding