Healthkit Script Doesn't Work

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)
1 Like

Await for my response as I’ll refactor this and add helpful comments/tips throughout the source code.

1 Like

is it server of client script???
if is client u need to use remote event to fire a messgae to server to do the healing


First insert a script exactly like this, directly under the tool. (Call it whatever you want)
image
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.


(Sorry for my bad drawing)

Here’s the code:

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)

2 Likes

Very Sorry I couldn’t get to you because I wasn’t getting any notifications for some reason but let me test it out real quick.

U can put math.huge if you want it to be inf