Hello there users of devforum. I am making a simple script of the player just clicking to heal with a tool. The script although does perform fine it doesn’t really “heal” the player and instead insists on returning the health to what it was before. For example, if the player has 60 health, I activate the script and add +25 health, but soon after it just returns it back to 60.
I don’t seem to understand whats going on here.
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
wait(1)
local humanoid = character.Humanoid
local tool = script.Parent
local maxuses = tool.Value
local debounce = false
tool.Activated:Connect(function()
if not debounce and humanoid then
debounce = true
humanoid.Health = 100
maxuses.Value = maxuses.Value - 1
print("Used")
if maxuses.Value == 0 then
tool:Destroy()
end
end
wait(5)
debounce = false
end)
First insert a script exactly like this, directly under the tool. (Call it whatever you want)
It should be a script instead of a local script since it isn’t replicated throughout the server (but health change fires an event), so it resets your health because the server is like: "Hey! That’s not the health I have on my side, are you cheating me?
And then the server is like okay I need to correct this, so it instantly changes it back to the original health in about ~0.01s. This goes with most properties of the humanoid.
local Tool = script.Parent
local HealAmount = 20 -- // Could change this to 100 since MaxHealth will just set it to full health.
local MaxUses = 3 -- // Change to whatever you want, will destroy after this many uses.
local DelayAmount = 5 -- // Change to whatever you want, will delay use.
local Deb = false
Tool.Activated:Connect(function()
local Character = Tool.Parent
local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
if Deb or not Humanoid then return end -- This is called a guarded if statement, it makes code a lot cleaner, we end the script as soon as any of these are true/false.
Deb = true
Humanoid.Health += HealAmount
MaxUses -= 1
print("Used")
if MaxUses == 0 then
Tool:Destroy()
end
task.wait(DelayAmount) -- task.wait() is a new better alternative for "wait()" look up benefits if you are interested.
Deb = false
end)