Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to add a delay so you can't use the animation repeatedly?

I want to make my animation wait for a couple of seconds after I input my key. But when I tried using wait(number here) it kinda worked.. So when i input my key it waits a couple seconds, and then if you click multiple times during the wait it makes it so that it plays the animation multiple times. Here's the code below.

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:wait()
local canattcak = script:WaitForChild("CanAttack")
local larm = char:WaitForChild("LeftHand") 
local rarm = char:WaitForChild("RightHand")

local hum = char.Humanoid

player = game.Players.LocalPlayer
 
mouse = player:GetMouse()
 
animation1 = script:WaitForChild("Animation1")
animation2 = script:WaitForChild("Animation2")
 
enabled = true
 
 
mouse.Button2Down:connect(function()
	local choose = math.random(1,2)
	if choose == 1 then
        enabled = false
canattcak = true
        local animationTrack = player.Character.Humanoid:LoadAnimation(animation1)
        animationTrack:Play()
        wait(1)
canattcak = false
        enabled = true
	elseif choose == 2 then
mouse.Button2Down:connect(function()
        enabled = false
canattcak = true
        local animationTrack = player.Character.Humanoid:LoadAnimation(animation2)
        animationTrack:Play()
        wait(1)
canattcak = false
        enabled = true

end)
end
end)

1 answer

This one may work for you...

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:wait()
local canattcak = script:WaitForChild("CanAttack")
local larm = char:WaitForChild("LeftHand") 
local rarm = char:WaitForChild("RightHand")
local attacking = false

local hum = char.Humanoid

player = game.Players.LocalPlayer

mouse = player:GetMouse()

animation1 = script:WaitForChild("Animation1")
animation2 = script:WaitForChild("Animation2")

enabled = true


mouse.Button2Down:connect(function()
if attacking == false then
    local choose = math.random(1,2)
    if choose == 1 then
        enabled = false
canattcak.Value = true
attacking = true
        local animationTrack = player.Character.Humanoid:LoadAnimation(animation1)
        animationTrack:Play()
        wait(1)
attacking = false
canattcak.Value = false
        enabled = true
    elseif choose == 2 then
        enabled = false
canattcak.Value = true
attacking = true
        local animationTrack = player.Character.Humanoid:LoadAnimation(animation2)
        animationTrack:Play()
        wait(1)
attacking = false
canattcak.Value = false
        enabled = true
end
end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.