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…
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.
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
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)
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
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!
@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. to
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!
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)
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: