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

Healing script resets the health added to the player?

Asked by 3 years ago

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)
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) RAFA1608 543 — 3y
0
I'd suggest for you to do the script in a serverscript instead. RAFA1608 543 — 3y
0
This is what I suspected, but when I copy paste the code into a serverscript, I get this error. MikelTheWolf 2 — 3y
0
Workspace.Food.Script:3: attempt to index nil with 'Character' - Server - Script:3 MikelTheWolf 2 — 3y
View all comments (2 more)
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. sngnn 274 — 3y
0
The localscript will only heal the player on their screen, but for the others the player is not healed to them GameBuilderLol 58 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

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

Ad

Answer this question