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)
Alright so I messed up the first time but I think this should work now. (explanations in script)
local debounce = false -- debounce is outside the script local debounce2 = false -- 2nd debounce for damage local anim = Instance.new("Animation", workspace) -- animation is outside so you don't make a new instance every time you click anim.AnimationId = "rbxassetid://5821099556" -- animationID game.ReplicatedStorage.Chop.OnServerEvent:Connect(function(plr) -- on serverevent local rArm = plr.Character:WaitForChild("Right Arm") -- waiting for right arm local hum = plr.Character:WaitForChild("Humanoid") -- waiting for humanoid local animplay = hum:LoadAnimation(anim) if not debounce then -- if debounce == false then debounce = true debounce2 = false -- debounce2 is now false so damage can be used animplay:Play() -- playing animation wait(5) -- make sure this wait is how much time it will take for your animation to end debounce = false -- debounce = false end rArm.Touched:Connect(function(hit) -- when arm hits if not debounce2 and debounce then -- checking if animation is playing and if you're able to damage debounce2 = true -- now you can't damage anymore if hit.Parent:FindFirstChild("Humanoid") then -- checking if what you hit is a humanoid hit.Parent.Humanoid:TakeDamage(45) -- makes them take damage end end end) end)