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.