So I am trying to make a health adding tool. Everything works, except the fact that the max uses doesn’t work, meaning you can use it infinitely. Here’s my script.
local uses = 0
local maxUses = 5
script.Parent.Equipped:Connect(function(Mouse)
Mouse.Button1Down:Connect(function()
local Animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.EatAnim)
local h = script.Parent.Parent:FindFirstChild("Humanoid")
if h then
if uses <= maxUses then
uses = uses + 1
print(uses)
h.Health = h.Health + 3
Animation:Play()
else
script.Parent:Destroy()
end
end
script.Parent.EatSFX:Play()
script.Disabled = true
wait (1.5)
script.Disabled = false
end)
end)
local uses = 0
local maxUses = 5
local connection
script.Parent.Equipped:Connect(function(Mouse)
if connection then connection:Disconnect() end
connection = Mouse.Button1Down:Connect(function()
local Animation = game.Players.LocalPlayer.Character.Humanoid.Animator:LoadAnimation(script.Parent.EatAnim)
local h = script.Parent.Parent:FindFirstChild("Humanoid")
if h then
if uses < maxUses then
uses += 1
h.Health = h.Health + 3
Animation:Play()
else
script.Parent:Destroy()
end
end
script.Parent.EatSFX:Play()
script.Disabled = true
wait (1.5)
script.Disabled = false
end)
end)
You’re doing a lot of bad practices such as having connections within connections (without disconnecting. This leads to memory leaks), trying to change players health on a local script, etc.
I reformated and simplified your script. I explain line by line the thought process:
Note that the below is a SCRIPT not a local script.
--[[
Note that you were doing this from a local script before. I know it seems like you were changing your health but in reality it only looked like that for your client.
Health doesn't replicate to the server. So you have to adjust health in a server script!!
--]]
-- Configs --
local MaxUses = 5; -- Max uses a player can have
local HealthAmount = 20; -- The amount they get healed
local Cooldown = 1.5; -- The cool down the player has to wait before healing again
-- Variables --
local Debounce = false; -- This is so player doesn't spam click and has to wait to gain health again!
local CurrentUses = 0;
local Tool = script.Parent;
local Player = script.Parent.Parent.Parent; -- This script runs as soon as its parented to the backpack!
-- Connections --
Tool.Activated:Connect(function() -- Fires when the player clicks/activates the tool
if not Debounce then -- If the debounce is false then it runs
Debounce = true; -- We now set the debounce to true -> meaning if the player clicks again, the if statement above doesnt fire
local Character = Player.Character or Player.Character:Wait();
local Humanoid = Character:WaitForChild("Humanoid");
local Animation = Humanoid:LoadAnimation(script.Parent.EatAnim);
if CurrentUses < MaxUses then -- Check if current uses are still less then the max
CurrentUses += 1;
print(CurrentUses);
Humanoid.Health = Humanoid.Health + HealthAmount;
Animation:Play();
script.Parent.EatSFX:Play();
elseif CurrentUses >= MaxUses then -- If the current uses is greater or equal to max uses. Then we destroy the tool!
Tool:Destroy();
end;
task.wait(Cooldown); -- Wait the cool down!
Debounce = false; -- Then we set the debounce back to false so the function can fire again!
end;
end);
-- its that easy!
One small problem. The tool only heals if you start with the item. For example. If you were to put it inside ReplicatedFirst and wanted to clone it, It wouldn’t work. Do you have any suggestions to fix this?
Post the error. Also you shouldn’t store stuff in replicatedFirst. ReplicatedStorage is the correct place to store items (that you want both the client and server to have access too).
It seems that there’s no error that’s coming up on output. Just when I click, nothing happens. Even when I try to search for that certain script in output, and nothing.