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

Script to change players health when activated not working?

I'm making a first aid kit and it almost all works properly, adds to the players health but as soon as it adds to the players health less than a second later the players health goes back down to where it originally was.

local Tool = script.Parent 
local Player = game:GetService("Players").LocalPlayer.Name
local healAmount = 30
local sound = script.Parent:FindFirstChild("FirstAid")
print(Player)

Tool.Equipped:Connect(function(Mouse)
	Mouse.Button1Down:Connect(function()
		print("Button1Down")
		local Playeruser = game.Workspace:WaitForChild(Player)
		local humanoid = Playeruser:FindFirstChildWhichIsA("Humanoid")
			local currentHealth = humanoid.Health
			local newHealth = currentHealth + healAmount
			humanoid.Health = newHealth
			sound:Play()
		wait(3.344)
		Tool:Destroy()
		

	end)
end)

Could someone tell me why? Thanks!

2 comments

0

Is your script local? You can only change health on the server.

0

Yeah it is, but how would I get the player with the server script

1 answer

✓ Accepted answer

You should be doing this on the Server, not Client.

local Tool = script.Parent
local healAmount = 30
local Sound = Tool:WaitForChild('FirstAid')
local beenUsed = false

local Players = game:GetService('Players')

Tool.Activated:Connect(function()
	if beenUsed then return end -- can only use once
	beenUsed  = true
	local Player = Players:GetPlayerFromCharacter(Tool.Parent)
	if Player then
		if Player.Character and Player.Character:FindFirstChild('Humanoid') then
			local Humanoid = Player.Character.Humanoid
			Humanoid.Health = Humanoid.Health + healAmount
			Sound:Play()
			wait(Sound.TimeLength)
			Tool:Destroy()
		end -- core checks
	end -- player check
end) -- onEquipped

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