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

Health Tool Script?

Asked by
JJ_B 250 Moderation Voter
9 years ago

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.

0
Output says "R is not a valid member of Players" JJ_B 250 — 9y
0
"Now the output tells me I'm attempting to index a nil value on line four. Need more help." -- this means that there is no model named the same as script.Parent.Parent.Name in the workspace. What is script.Parent.Parent.Name ? Try printing 'r' between lines 3 and 4 chess123mate 5873 — 9y

2 answers

Log in to vote
2
Answered by 9 years ago

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.

0
See edit to question JJ_B 250 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

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)
0
Doesn't work for some reason, output says "Character is not a valid member of Workspace" JJ_B 250 — 9y
0
Try it now. It now automatically adapts to any player who picks it up (instead of healing the player it started with -- and in your case, it sounds like it started in the workspace). chess123mate 5873 — 9y

Answer this question