So I am newish to scripting, and was wondering if you could help me straighten out a few grey areas in this health pack script form roblox wiki.
Here is the script:
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)
Here are my questions:
For The Paramater otherPart (Line 6) how does the computer know what role otherPart plays? I think, that otherPart is the equivalent of the hit Parameter on a touched function, but I'm not sure how the computer can just pick up on the fact that otherPart = whatever touches the health pack, without a touched event before it?
So for this one I just want so clarification, the canHeal variable (lines 4,9,13,20), is just another wording for debouncing. There is nothing special about the word "debounce" itself. Because if you type in canHeal the text is still black, but if you type debounce the text turns green.
And Finally, is may just be some error, but why is canHeal set to false, after we define that the player needs healing, and why do we heal the player, after canHeal is set to false?
I hope that you can provide me with the answers I need, and either way I thank you for your time and effort.

4 comments
Lmao dude thats a lot of typing, earned an upvote
Amazing explanation
Thanks so much, this was very helpful.
No Problem