How would I add a cooldown to one of these scripts? I need it t where the player cannot spam left click and punch. The animations don't finish if they spam it
local plr = game.Players.LocalPlayer local char = plr.Character or plr.CharacterAdded:wait() local tool = script.Parent local swingsound = tool:WaitForChild("Swing") local canattack = tool:WaitForChild("CanAttack") tool.Activated:connect(function() local choose = math.random(1,4) canattack.Value = true if choose == 1 then local swing1animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch1) swingsound:Play() swing1animation:Play() elseif choose == 2 then local swing2animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch2) swingsound:Play() swing2animation:Play() elseif choose == 3 then local swing3animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch3) swingsound:Play() swing3animation:Play() elseif choose == 4 then local swing4animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch4) swingsound:Play() swing4animation:Play() end end) tool.Deactivated:connect(function() canattack.Value = true end) tool.Unequipped:connect(function() local hum = char:WaitForChild("Humanoid") for _,anim in pairs(hum:GetPlayingAnimationTracks()) do anim:Stop() end end)
local tool = script.Parent local hitsound = tool:WaitForChild("Hit") local canattack = tool:WaitForChild("CanAttack") local plr = tool.Parent.Parent local char = plr.Character or plr.CharacterAdded:wait() local larm = char:WaitForChild("Left Arm") -- this can be changed im only using this for the R6 body type local rarm = char:WaitForChild("Right Arm") local rleg = char:WaitForChild("Right Leg") local lleg = char:WaitForChild("Left Leg") local dmg = 5*10-- this can be changed im only using this for the R6 body type larm.Touched:connect(function(hit) local hum = hit.Parent:FindFirstChild("Humanoid") if canattack.Value == false then if hum then canattack.Value = true hitsound:Play() hum:TakeDamage(dmg) -- u can change this value wait(1) canattack.Value = false end end end) rarm.Touched:connect(function(hit) local hum = hit.Parent:FindFirstChild("Humanoid") if canattack.Value == false then if hum then canattack.Value = true hitsound:Play() hum:TakeDamage(dmg) -- u can change this value wait(1) canattack.Value = false end end end) lleg.Touched:connect(function(hit) local hum = hit.Parent:FindFirstChild("Humanoid") if canattack.Value == false then if hum then canattack.Value = true hitsound:Play() hum:TakeDamage(dmg) -- u can change this value wait(1) canattack.Value = false end end end) rleg.Touched:connect(function(hit) local hum = hit.Parent:FindFirstChild("Humanoid") if canattack.Value == false then if hum then canattack.Value = true hitsound:Play() hum:TakeDamage(dmg) -- u can change this value wait(1) canattack.Value = false end end end)
Debounce is the best solution for this
local plr = game.Players.LocalPlayer local char = plr.Character or plr.CharacterAdded:wait() local tool = script.Parent local swingsound = tool:WaitForChild("Swing") local canattack = tool:WaitForChild("CanAttack") local debounce = false -- set the debounce value tool.Activated:connect(function() if debounce == false then -- check if debounce is false local choose = math.random(1,4) canattack.Value = true if choose == 1 then debounce = true -- set debounce as true local swing1animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch1) swingsound:Play() swing1animation:Play() wait(delayForDebounce) -- wait debounce = false -- set debounce as false so the code can run again elseif choose == 2 then debounce = true -- set debounce as true local swing2animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch2) swingsound:Play() swing2animation:Play() wait(delayForDebounce) -- wait debounce = false -- set debounce as false so the code can run again elseif choose == 3 then debounce = true -- set debounce as true local swing3animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch3) swingsound:Play() swing3animation:Play() wait(delayForDebounce) -- wait debounce = false -- set debounce as false so the code can run again elseif choose == 4 then debounce = true -- set debounce as true local swing4animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch4) swingsound:Play() swing4animation:Play() wait(delayForDebounce) -- wait debounce = false -- set debounce as false so the code can run again end end end) tool.Deactivated:connect(function() canattack.Value = true end) tool.Unequipped:connect(function() local hum = char:WaitForChild("Humanoid") for _,anim in pairs(hum:GetPlayingAnimationTracks()) do anim:Stop() end end)
Hope it helps :D