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

Insert a Delay on a Function?

Asked by 4 years ago
Edited 4 years ago

Hi, i have a little problem:

(sorry for bad english). I need to insert a delay in this function for 0.5 Seconds. You can activate this function only 1 time for every 0.5 seconds. It's simple, but i don't know how i can do it >.<.

tool.Activated:connect(function()
    local choose = math.random(1,2)
    canattack.Value = true
    if choose == 1 then
        local swing1animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch1)
        swing1animation:Play()
        wait(.2)
        swingsound:Play()
    elseif choose == 2 then
        local swing2animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch2)
        swing2animation:Play()
        wait(.2)
        swingsound:Play()
    end
end)

Thanks!

2 answers

Log in to vote
1
Answered by 4 years ago

Change your script to look like this (look under it for the explanation):

local canSwing = true
local DEBOUNCE_TIME = 0.5
tool.Activated:connect(function()
    if canSwing then
        canSwing = false
        local choose = math.random(1,2)
        canattack.Value = true
        if choose == 1 then
            local swing1animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch1)
                swing1animation:Play()
                wait(.2)
                swingsound:Play()
        elseif choose == 2 then
                local swing2animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch2)
                swing2animation:Play()
                wait(.2)
                swingsound:Play()
        end
        spawn(function()
            wait(DEBOUNCE_TIME)
            canSwing = true
        end)
    end
end)

We use spawn() to create a new thread (basically as if you are running it on another script, but can access the variables in the script) and run our wait function in there. We then see if canSwing is true (we are swinging for the first time or we have waited DEBOUNCE_TIME amount of time) and, if it is, run the function you made.

Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

Put a Boolean Value inside the Main Script called 'delay', make sure it's false. The boolean value will help the second script able to turn the cooldown into a variable that can be accessed by the main script.

-- Main Script
if script.delay.Value == false then
tool.Activated:connect(function()
    local choose = math.random(1,2)
    canattack.Value = true
    if choose == 1 then
        local swing1animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch1)
        swing1animation:Play()
        wait(.2)
        swingsound:Play()
        script.delay.Value = true
        script.cooldown.Disabled = false
    elseif choose == 2 then
        local swing2animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch2)
        swing2animation:Play()
        wait(.2)
        swingsound:Play()
        script.delay.Value = true
        script.cooldown.Disabled = false
   end
end)
end

Put a Server Script inside the Main Script called 'cooldown' and make sure it's disabled. The cooldown can help set the delay back to false giving a pseudo-cooldown for your Main Script.

-- Second Script
if script.Parent.delay.Value == true then
    wait(.5)
    script.Parent.delay.Value = false
    script.Disabled = true
end
0
lol get moderated boi KDarren12 705 — 4y
0
very weird way of doing the problem, you should use spawn() instead throwawayaccount2001 102 — 4y
0
well we're all different aren't we. ScaleisRed 58 — 4y

Answer this question