My fists are supposed to do a large amount of damage and only attack once, but with my fists they attack forever even if you unequip them. This is probably related to how simple the code is and how poorly I scripted it, but any help is appreciated.
local Tool = script.Parent
local Hitbox = Tool.Handle
local Damage = 100
Tool.Activated:Connect(function()
Hitbox.Touched:Connect(function(hit)
local Humanoid = hit.Parent:FindFirstChild("Humanoid")
if Humanoid then
task.wait(1)
Humanoid.Health -= Damage
end
end)
end)
Every time the tool is activated it executes the function. The function creates a connection every time it is executed, and it is never disconnected. To disconnect it you need to store it in a variable:
local conn = Hotbox.Touched:Connect([...])
And then you need to disconnect it by doing conn:Disconnect(). There is probably a tool unequipped event which would be suitable for that.
local Tool = script.Parent
local Hitbox = Tool.Handle
local Damage = 100
Tool.Activated:Connect(function()
local conn = Hitbox.Touched:Connect(function(hit)
local Humanoid = hit.Parent:FindFirstChild("Humanoid")
if Humanoid then
task.wait(1)
Humanoid.Health -= Damage
end
end)
task.wait(1.1) -- Make sure the connection doesnt disconnect early
conn:Disconnect()
end)