Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Health Tool Script?

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.

2 comments

0

Output says "R is not a valid member of Players"

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

2 answers

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.

1 comment

0

See edit to question

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)

2 comments

0

Doesn't work for some reason, output says "Character is not a valid member of Workspace"

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).

This discussion is preserved as it was. New answers, comments, and votes are closed.