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

Healing script resets the health added to the player?

Right now, I have created a tool that, when activated, gives the player 10 health. However, a second after giving the player the health, it takes it away. Does anyone know why this is the case? It is a localscript placed inside a tool named "Food"

local tool = script.Parent
local player = game.Players.LocalPlayer 
local char = player.Character 
local healAmount = 10

function onActivated ()
	print("Hello")
	local currentHealth = char.Humanoid.Health
	local newHealth = currentHealth + healAmount
	char.Humanoid.Health = newHealth
end

script.Parent.Activated:Connect(onActivated)

6 comments

0

Due to filtering enabled, localscripts can't and won't interact with the game (i.e. other players won't be able to see the modifications your localscript made)

0

I'd suggest for you to do the script in a serverscript instead.

0

This is what I suspected, but when I copy paste the code into a serverscript, I get this error.

0

Workspace.Food.Script:3: attempt to index nil with 'Character' - Server - Script:3

0

You can't use LocalPlayer inside of a server script. Instead, try checking for the tool's parent inside of the function to get the character.

0

The localscript will only heal the player on their screen, but for the others the player is not healed to them

1 answer

Try this:

local tool = script.Parent
local healAmount = 10

function onActivated (player)
    print("Hello")
    local char = player.Character
    local currentHealth = char.Humanoid.Health
    local newHealth = currentHealth + healAmount
    char.Humanoid.Health = newHealth
end

script.Parent.Activated:Connect(onActivated)

Put this on a server script

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