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

Why doesnt this script change the humanoids health?

Asked by 4 years ago

Hey guys i made this script earlier today and its meant to work as a healing pad for players when they touch it.


local Medkit = script.Parent local function onPartTouch(hit) local Player = hit.Parent:WaitForChild("Humanoid") local hp = Player.Health local maxhp = Player.MaxHealth -- local healAmount = (maxhp - hp) if Player then hp = maxhp --(Player.Health + healAmount) print ("hit") wait (1) end end Medkit.Touched:Connect(onPartTouch)

It detects the player correctly as i get the print in the output however it doesn't change the players health. I have compared my script with other peoples functioning versions but I still don't understand the problem with mine. Any help would be much appreciated, thanks.

2 answers

Log in to vote
0
Answered by 4 years ago

I'm revising my answer with the hopes that it will help you learn.

local Medkit = script.Parent
local Debounce = true

local function onPartTouch(hit)
    if (not Debounce) then return; end --//This is a part of the debounce, we're setting a debounce, that just allows the part to be touched every one second

    local Humanoid = hit.Parent:FindFirstChild("Humanoid") --//We see if we can find the Humanoid inside the parent of the part that touches the medkit

    if (Humanoid) then --//We found it
        Debounce = false --//We're setting the debounce to false to the touched event can no longer fire while we do what we need
        Humanoid.Health = Humanoid.MaxHealth --//Setting the health to the maxhealth of the humanoid

        wait(1) --//You can change this to however long you want to wait before touching the medkit again to get more health

        Debounce = true --//Changing the debounce to true so we can touch the part again
    end
end

Medkit.Touched:Connect(onPartTouch)

I've added in a lot of comments, I hope you'll read them. All I'm doing is optimizing your code and this should do exactly what you want it to do. If you have any more problems just tell me.

1
Thanks for putting in the extra effort optimizing and putting in the comments the script is working well now. StarDestroy 25 — 4y
0
Glad I could help! CeramicTile 847 — 4y
Ad
Log in to vote
0
Answered by
seikkatsu 110
4 years ago

okay so i think the issue is that you want to change the health of the PLAYER not the HUMANOID wich contains the 'helath' property. First you have to get the character and then the humanoid

Please consider accepting my answer by pressing the 'accept answer' button and hit me up through the comments if you need help have a nice day

1
Lol, yes he wants to change the health of the PLAYER and to do that he needs to change the health property of the HUMANOID and currently he isn't changing the health of the HUMANOID but he is just changing a variable set to the health of the HUMANOID. CeramicTile 847 — 4y
1
But seriously that first sentence made me think I had a stroke and didn't make any sense.. CeramicTile 847 — 4y
1
Health isn't a property of Player lol Robowon1 323 — 4y

Answer this question