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

Health modifier wont work?

Asked by
hoth7 45
3 years ago

Hello everybody, I'm trying to make a health pad script kind of like in fencing, but it wont work so if you could respond with a answer it would be greatly appreciated.

function onTouch (hit)
    game.Players.LocalPlayer.Humanoid.Maxhealth = 200
    game.Players.LocalPlayer.Humanoid.Health = 200
end

script.Parent.Touched:Connect(onTouch)

(i've only been scripting for 2-3 years so i'm kind of still new.)

2 answers

Log in to vote
2
Answered by
tjtorin 172
3 years ago

In your script you are getting the LocalPlayer which is you cannot do in a server script and that is only the player not the character so you cannot get the humanoid from it. Instead you can use the hit argument in your onTouch function to get the humanoid. hit is the object that it touched so if a players leg touched it you can say hit.Parent to get the character and then get the humanoid inside of the character. You should always check if the thing touching it is a character by checking if they have a humanoid so it does not error because it can tell if other things are touching it like another part.

function onTouch (hit)
    local humanoid = hit.Parent:FindFirstChild("Humanoid")
    if humanoid then
        humanoid.MaxHealth = 200
        humanoid.Health = 200
    end
end

script.Parent.Touched:Connect(onTouch)

I would also recommend that you check if the character has already used the health pad so they are not able to heal infinitely. You could do that by adding a value to the character and the next time that character touches they pad check if they have that value.

function onTouch (hit)
    local humanoid = hit.Parent:FindFirstChild("Humanoid")
    if humanoid and not hit.Parent:FindFirstChild("TouchedHealthPad") then
        local tag = Instance.new("BoolValue") -- Does not really matter what type of value it is because only the name matters
        tag.Name = "TouchedHealthPad"
        tag.Parent = hit.Parent

        humanoid.MaxHealth = 200
        humanoid.Health = 200
    end
end

script.Parent.Touched:Connect(onTouch)

If you would like the player to be able to get the boost from the health pad again after some time then you can add this after creating the tag:

game.Debris:AddItem(tag, 15) -- This will delete the tag in 15 seconds
0
And which line would i put game.Debris:AddItem(tag, 15) on? hoth7 45 — 3y
0
you would put it right after tag.Parent = hit.Parent tjtorin 172 — 3y
0
Hmm, I did that and whenever the bool value goes away the first time, i touch the hit pad on 1 hp and only just gives me the bool value again. hoth7 45 — 3y
0
Is there a way i could modify the script to use debounce instead? hoth7 45 — 3y
View all comments (2 more)
0
I just tested and it does work. I assume you are changing the health of the humanoid while in studio playing the game and that only changes it on the client not the server. You have to make sure you are in server mode before you change the health. tjtorin 172 — 3y
0
ok, thanks. hoth7 45 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago
function onTouch (hit)
    game.Players.LocalPlayer.Character.Humanoid.MaxHealth = 200
    game.Players.LocalPlayer.Character.Humanoid.Health = 200
end

script.Parent.Touched:Connect(onTouch)

Humanoid isn't in the Players thing, it is in the player model in Workspace, so you have to do .Character

0
I tried that, didnt work hoth7 45 — 3y

Answer this question