Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
R_G0d -2
6 years ago

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

Log in to vote
0
Answered by 6 years ago

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)
Ad

Answer this question