Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

A Few Things I Can't Understand In This Health Pack?

Asked by 6 years ago

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.

1 answer

Log in to vote
5
Answered by
Zafirua 1348 Badge of Merit Moderation Voter
6 years ago
Edited 6 years ago

I began scripting from learning from Free Models too so I can definitely understand your problem here. Let's look into your questions

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?

Yes, the other part is equivalent to hit. But it does not matter what variable you type in there. But for our purposes, we will stick to OtherPart.

The following line of the code is not correct.

local character = otherPart.Parent
print(character)

Why?

Because OtherPart's Parent can be anything. When you insert a Part in Baseplate, the Part is still touching the Baseplate. Thus the code above would print Workspace instead.

To counter that, we have to make sure that whatever is touching the Part, it is Local Player

if otherPart.Parent:FindFirstChild("Humanoid") then 

end 

You also are connecting the code separately. Which is why you do not need the Touched Event beside the function. If you did not have Line 24 your code would not run or do anything because the function has not been called. The Line 24 calls the function, uses the Event called Touched and processes your code.

the canHeal variable (lines 4,9,13,20), is just another wording for debouncing.

That is correct. The CanHeal variable here is used to check if the Player is eligible to gain health or not. If not, they will not receive health and if they do, then they receive health.

Because if you type in canHeal the text is still black, but if you type debounce the text turns green. I am not really sure on what this statement is about since it has never occurred to me

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?

One of the biggest problems I have seen with Touched Event.

Consider the following code

-- Declaration Section 
local Workspace = game:GetService("Workspace")
local Part = Workspace:WaitForChild("Part")

local function PrintTouchParts (Hit)
    print(Hit)
end 

Part.Touched:Connect(PrintTouchParts)

Run the above code and see the output. You will notice that Hit prints quite a lot of times. This is the biggest flaw of Touched Event and the reason why CanHeal is set to false.

The code specifically checks if CanHeal is true

if humanoid and canHeal then

This is saying that if CanHeal == true, then the code will run.

However, what are the probability of Player moving around the parts. Wouldn't the function run a lot of times as seen by the example above?

This is why we immediately change the CanHeal to false. In that case, the if statement is not true and thus won't evaluate.

I hope you learned something from this.

Have a lovely day of coding!
0
Lmao dude thats a lot of typing, earned an upvote cmgtotalyawesome 1418 — 6y
0
Amazing explanation saSlol2436 716 — 6y
0
Thanks so much, this was very helpful. FlippinAwesomeCrew 62 — 6y
0
No Problem Zafirua 1348 — 6y
Ad

Answer this question