Why won't this work?
r = script.Parent.Parent.Name function heal() local health = game.Players.r.Humanoid.Health health = health+50 script.Parent.Handle.Sound:play() wait(5) end script.Parent.Activated:connect(heal)
Please help! Thanks
EDIT: I edited the script according to CreativeEnergy's answer to the following:
r = script.Parent.Parent.Name function heal() local h = game.Workspace:FindFirstChild(r).Humanoid.Health h = h+50 script.Parent.Handle.Sound:Play() end script.Parent.Activated:connect(heal)
Now the output tells me I'm attempting to index a nil value on line four. Need more help.
This is the 3rd time I've had to retype this answer and I'm on a phone so I will get to the point.
You're trying to find the character in players how you have it now, so we can change line 4 to
game.Workspace:FindFirstChild(r).Humanoid.Health
Note: This is in assumption that the characters name is unique in the workspace.
In addition to what CreativeEnergy says:
-You could also use game.Players[r].Character.Humanoid.Health
instead of using FindFirstChild
-Since script.Parent.Parent is probably the Player, you could just use a "player" variable.
-You must reference the humanoid and increase its .Health value. At the moment, you're just increasing your local health variable.
-wait(5) does nothing if you don't use "debounce".
Fixed function:
db = false --debounce function heal() if db then return end --prevent multiple instances of the function from running simultaneously local humanoid = script.Parent.Parent:FindFirstChild("Humanoid") if humanoid == nil then return end humanoid.Health = humanoid.Health + 50 script.Parent.Handle.Sound:play() db = true --normally this is put right after the "if db then return end" line, but it's fine so long as we put it before the wait() command. We mustn't let "db" be true if we return early (ex if the humanoid is nil). wait(5) db = false end script.Parent.Activated:connect(heal)