local AbleToDamage = true local Damage = 30 script.Parent.Handle.Touched:Connect(function(Hit) script.Parent.Activated:Connect(function() if AbleToDamage == false then return end AbleToDamage = false if Hit.Parent:FindFirstChild("Humanoid") then Hit.Parent.Humanoid:TakeDamage(Damage) end wait(0.5) AbleToDamage = true end) end)
I know that the problem is that the hit value saves. How do I fix it so it looks for a new value each time I activate the tool?
local AbleToDamage = true local Damage = 30 local function Activated () script.Parent.Handle.Touched:Connect(function(Hit) if AbleToDamage == true then AbleToDamage = false local Humanoid = Hit.Parent:FindFirstChildWhichIsA("Humanoid") if Humanoid then Humanoid:TakeDamage(Damage) end wait(0.5) AbleToDamage = true end end end script.Parent.Activated:Connect(Activated)
I noticed a problem, if you activate the tool it would listen for the touched
event but after that it would still continue to listen for the touched
event, so if your still holding the tool if someone touches it, it would damage them. I didn't really know what was wrong with it, so i tried to re-create it and while doing so i also tried to "improve" it.
You can try this, i added something so that it doesn't still listen for the touched
event after its activated.
local AbleToDamage = true local Damage = 30 local Connection local function Activated () Connection = script.Parent.Handle.Touched:Connect(function(Hit) if AbleToDamage == true then AbleToDamage = false local Humanoid = Hit.Parent:FindFirstChildWhichIsA("Humanoid") if Humanoid then Humanoid:TakeDamage(Damage) end Connection:Disconnect() wait(0.5) AbleToDamage = true else Connection:Disconnect() end end) end script.Parent.Activated:Connect(Activated)