Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I can't figure out how to make a working first aid pack?

I've been trying to figure out how to script a working first aid pack that regenerates a specific amount of health each time it's picked up, but I just can't get it right.. Could someone please help me out?

1 answer

✓ Accepted answer

First, you need to build your model.

Your model needs a part to call the touch function on. I will name mine "Base".

I will keep my pickup simple by simply having one base and a floating item that resembles the type of power up.

Ok. We have a pickup model but it doesn't do anything yet. We need to script it.

Insert a script (NOT a localscript).

Make sure that the script's parent is the model.

Now we need to store the Base in a variable and call a function every time the part is touched.

The function we will use for this is Touched.

Base.Touched:connect(function()

end)

Okay. Now we can detect when the base is touched but, how do we know if what touched it is a player?

The touched function has a parameter we can use that gets the part that touched it.

Base.Touched:connect(function(Hit) -- The hit parameter to get the part that touched.

end)

We now have a parameter that can tell you what touched it. This parameter will not tell us which player touched it. To do this we need to add some code to find the player.

If the object that touched it is a player then Hit's parent will be the player's Character.

Base.Touched:connect(function(Hit)
	-- We need to get the human of the player
	local Human = Hit.Parent:FindFirstChild("Humanoid")
	-- Now we need to see if the humanoid exists.
	if Human then -- If we find a human
		local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
		if Player then -- If we find a humanoid and the human is a player
			
		end
	end
end)

Now we can tell if what touched the base is a player. Now we need to add to the player's current health.

local Base = script.Parent.Base
local PickUp = script.Parent.PickUp

PickUp.CanCollide = false
PickUp.Anchored = true -- Make sure that the pickup doesn't fall and that the player can walk through it

local PickUpIsReady = true -- This will prevent the player from standing on the pickup and remain invincible because the pick up is ready instantly.

Base.Touched:connect(function(Hit)
	-- We need to get the human of the player
	local Human = Hit.Parent:FindFirstChild("Humanoid")
	-- Now we need to see if the humanoid exists.
	if Human then -- If we find a human
		local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
		if Player then -- If we find a humanoid and the human is a player
			if Human.Health > 99 then return end -- No need to use the powerup so cancel the function
			if PickUpIsReady == false then return end -- Debounce
			Human.Health = Human.Health + 30
			PickUpIsReady = false
			PickUp.Transparency = 1
			wait(10) -- The pickup will not be available for 10 seconds after use
			PickUp.Transparency = 0
			PickUpIsReady = true -- PickUp is now ready for use
		end
	end
end)

Your power up is now complete.

If you like this answer please be sure to accept it.

Edit:

Here is my pickup model. You may use it as an example. Health Powerup model

This discussion is preserved as it was. New answers, comments, and votes are closed.