Humanoid Health Resets when Changed

I am trying to make a tool that heals the player in game when they press “E”, however, whenever the health is added, it always resets.

You can see the behavior here:

The code I am using to detect the player’s clicks is below:

--For extra functions that are not related
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local UIS = game:GetService("UserInputService")

mouse.Button1Down:Connect(function()
	local target = mouse.Target
	if not target then return end
	
	local humanoid = target.Parent:FindFirstChild("Humanoid")
	if humanoid then 
		if humanoid.Health ~= 0 then
			humanoid.Health = math.min(humanoid.MaxHealth, humanoid.Health + 75)
			script.Healed:FireServer()
		end
	end
end)

--Actual code that describes behavior begins here
UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		player.Character.Humanoid.Health = math.min(player.Character.Humanoid.MaxHealth, player.Character.Humanoid.Health + 25)
		script.Healed:FireServer()
	end
end)

I haven’t been able to find anything about this on the internet, so I’ve resorted to posting this here. This behavior is also precedent in other games (I tested this in one of my other places) and applies to models on the creator store too.

This is my first time posting, so please correct me if I did anything that is bad practice.

well yeah uh this code seems to be content of a LocalScript. this means the code is running clientsided aka. changes to the Humanoids Health are not recognized by the server. as the server still works with the old health value (lets say 75) it feels the need to execute measures to regenerate the players health. locally it looks like you have 100 health, as the server heals you to 76 health though, this is what the client recognizes, thinking you took damage, hence the short red flash and the reset to the old health value with now added regeneration health.

the fix: edit the health in a normal Script that runs serversided. i see you already fire a RemoteEvent so you could just do the health stuff in the script handling the call to the RemoteEvent

Thank you for replying so quickly! :smile:
I will apply it and see if it works.

EDIT: It did work. If anyone is having the same issue, here’s what I did:
I used the Heal remote event for sounds, so I created a new Self Healed event to trigger a server script that heals the player.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.