I created a combat script that has an animation. The animation works in the script, but I tried making it do damage and I also tried making a cooldown, but neither of those work. I'm able to spam the key instead of waiting before I can use it again. The damage also doesn't work because if I press the key and the animation isn't active, but I touch a humanoid it loses health. I just need help figuring out what I did wrong with the cooldown and the damage. This is the script:
game.ReplicatedStorage.Chop.OnServerEvent:Connect(function(plr)
local rArm = plr.Character["Right Arm"]
local debounce = false
local hum = plr.Character["Humanoid"]
local anim = Instance.new("Animation", workspace)
anim.AnimationId = "rbxassetid://5821099556"
local animplay = hum:LoadAnimation(anim)
animplay:Play()
rArm.Touched:Connect(function(hit)
if not debounce then
debounce = true
if hit.Parent:FindFirstChild("Humanoid") then
hit.Parent.Humanoid:TakeDamage(45)
wait(5)
debounce = false
end
end
end)
end)

5 comments
You have to put the local debounce at the very start of the script. Not inside the function.
Also recommend reading this: https://developer.roblox.com/en-us/recipes/How-to-disconnect-an-event-connection
When you don't have your tool out, it would still do damage through the arm because the event is still registered.
Ok I'll try that.
I moved local debounce at the start of the script, but I'm still able to spam the animation.