Why wont this script heal me anymore?

Hi, I made a script a few months ago. Its supposed to be used in tools so players can click and eat it. It should heal you everytime you click but it wont work anymore. I didnt change anything in the script but the healing wont work anymore. The whole script works but its only the healing that does not work anymore.

Heres the script:

local ClickDetector = script.Parent.Handle.ClickDetector
ClickDetector:Destroy()
local Tool = script.Parent
local EatAnimation = Tool.EatAnimation
local Value = 0
local Debounce = false

Tool.Activated:Connect(function()
	if Value == 3 then -- if the value is 3 the tool will get destroyed, so you can eat it only 3 times
		wait(2)
		Tool:Destroy()
	end	
end)

Tool.Equipped:Connect(function(Mouse)
	Tool.Activated:Connect(function()
		local Character = Tool.Parent
		local AnimationTrack = Character.Humanoid:LoadAnimation(EatAnimation)
		if Debounce == false then
			Debounce = true
		Value = Value + 1
		print(Value)
		AnimationTrack:Play()
			Character.Humanoid.Health = Character.Humanoid.Health + 50 -- this wont heal me anymore
			wait(2.5)
			Debounce = false
			
		end
	end)
end)

First off You see the function at line 8 copy the whole code and put it at the end of the code cause you can’t say if the Value is = 3 but before adding +1 every amount of given time…

1 Like

It still works the same if I put it at the end of the script but the main problem is that it wont give me the health that should be added to my health bar.

Send me the code via Discord also the Server and Local script ExoticAlpha#5755

I couldn’t find a solution yet, but I have a tip for you. If you want to add or subtract from something, you could do this:

Character.Humanoid.Health += 50

Instead of this:

Character.Humanoid.Health = Character.Humanoid.Health + 50

The same goes for subtraction, except you use -= instead of +=.

2 Likes

I’ve found the solution! In the video you can see you have about 80 health, and you’re trying to add 50 health to that. That isn’t possible, because you can only set the health between 0 and the player’s max health (in this case that’s 100), but you’re trying to set it to 130.

Solution:

-- instead of this
Character.Humanoid.Health = Character.Humanoid.Health + 50

-- do this
local humanoid = Character.Humanoid
local healHealth =  humanoid.Health + 50

if healHealth >= 100 then
humanoid.Health = 100
else
humanoid.Health = healHealth
end
2 Likes

somehow it still wont work I am trying to rewrite the whole script now but it just wont heal me :confused:

wait nvm it works but why does it not work when I damage myself by changing the value

local ClickDetector = script.Parent.Handle.ClickDetector
ClickDetector:Destroy()
local Tool = script.Parent
local EatAnimation = Tool.EatAnimation
local timesUsed = 0
local Debounce = false

Tool.Activated:Connect(function()
	if timesUsed == 3 then
		wait(2)
		Tool:Destroy()
	end	
end)

Tool.Equipped:Connect(function(Mouse)
	Tool.Activated:Connect(function()
		local Character = Tool.Parent
		local AnimationTrack = Character.Humanoid:LoadAnimation(EatAnimation)
		if Debounce == false then
			Debounce = true
		timesUsed += 1
		AnimationTrack:Play()
			local humanoid = Character.Humanoid
local healHealth =  humanoid.Health + 50

if healHealth >= 100 then
humanoid.Health = 100
else
humanoid.Health = healHealth
end
			wait(2.5)
			Debounce = false
			
		end
	end)
end)

Are you changing the health from the client or the server?

I have a damage part that damages me when I touch it. Then the healing works somehow but if I change the value by myself in the humanoid it wont heal me

Try changing it manually but from the server. It may not work because if you changed your health on the client, the server doesn’t know you did.

1 Like

You should mark the solution as the message where I explained why it didn’t work…

(in case other people have the same problem)

1 Like

I didnt know that this could be the reason why it doesnt work. That may also solve the problem with some other things. Thanks for the help

Hi there, just to make a correction,

This statement is incorrect, the Humanoid Object will automatically cap its own Health Property at its MaxHealth if incremented greater than its MaxHealth, (similarly to math.clamp()), no need to add any checks! :sweat_smile:

@enpiesie is correct on this! When working working with this tool specifically, you would want to use a Server Script, as well as change the values as the server in order to run this so that it will replicate throughout all clients. You can switch this in test mode by clicking the Current button on the top left, it should switch to Server.
image to image

I’ve also made something similar for some of my games, but with more various checks to ensure the tool works every time. I’ll post the code from my version of the tool here as an educational resource! :smile:

local Tool = script.Parent
local Character, Humanoid
local Equipped = false
local ToolEnabled = true
local Animation = script:WaitForChild("Animation")

--Variables
local HEAL_AMOUNT = 25
local COOLDOWN = 2.5
local USE_AMOUNT = 3
local CURRENT_USES = 0

--Function to run when tool is activated
Tool.Activated:Connect(function()
	if Character and Humanoid then
		if ToolEnabled then
			ToolEnabled = false
			Tool.Enabled = false
			
			--Checks to see if the CURRENT_USES is less than the USE_AMOUNT
			if CURRENT_USES < USE_AMOUNT then
				CURRENT_USES += 1 --Increment CURRENT_USES
				--Run Functions
				local animationTrack = Humanoid:LoadAnimation(Animation) --Load Animation
				animationTrack:Play() --Play Animation
				Humanoid.Health += HEAL_AMOUNT --Increment Humanoid's Health
				task.wait(animationTrack.Length) --Allow animation some time to play
			end
			
			--Unequips and Destroys Tool when USE_AMOUNT is reached
			if CURRENT_USES >= USE_AMOUNT then
				Humanoid:UnequipTools() --Unequip Tool
				Tool:Destroy() --Destroy the Tool
			end
			
			--Cooldown
			task.wait(COOLDOWN)
			ToolEnabled = true
			Tool.Enabled = true
		end
	end
end)

--Set up the equip event
Tool.Equipped:Connect(function()
	if not Equipped then
		Equipped = true
		--Set the current character
		Character = Tool.Parent
		Humanoid = Character:FindFirstChildOfClass("Humanoid")
	end
end)
--Set up the unequip event
Tool.Unequipped:Connect(function()
	if Equipped then
		Equipped = false
		--Reset the current character
		Character = nil
		Humanoid = nil
	end
end)
2 Likes

Oh, thanks for informing us about this. I just assumed it didn’t cap because of this:

Do you have any idea why the script didn’t work?

1 Like

I think they were changing the Health Value in test mode as a client which will not replicate to the server. That’s why the healing did work as stated above, when they were instead damaged from a part, as it most likely was a Server-Sided Script inside of a part, therefore damaging the Humanoid from the Server. When testing there is a feature to adjust values as the server shown in my previous post, just make sure in testing mode that the current mode is set to server!

Here is an article further explaining Roblox’s Client-Server Model:

1 Like