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

How to put debounces to multiple functions efficiently?

Asked by 4 years ago

So I'm trying to make a combat system with multiple attacks and I don't want players to be spamming the same attack over and over again.

Was:

01local UIS = game:GetService("UserInputService")
02 
03local cooldown = false
04 
05local function attack1()
06 
07    if not cooldown then
08        cooldown = true
09        --block of codes
10        wait()
11        cooldown = false
12    end
13 
14end
15 
View all 67 lines...

Attempt No. 1; I thought it was perfect but it didn't work. Please let me know if you do know why.

01local UIS = game:GetService("UserInputService")
02 
03local function attack1()
04    local cooldown = false
05 
06    if not cooldown then
07        cooldown = true
08        --block of codes
09        wait()
10        cooldown = false
11    end
12 
13end
14 
15local function attack2()
View all 69 lines...

Attempt No. 2

01local UIS = game:GetService("UserInputService")
02 
03local cooldown = false
04local cooldown2 = false
05local cooldown3 = false
06local cooldown4 = false
07 
08local function attack1()
09 
10    if not cooldown then
11        cooldown = true
12        --block of codes
13        wait()
14        cooldown = false
15    end
View all 70 lines...

Now, the second attempt actually did work. But I want to know if there's a more efficient/simple way to do it. Thank you.

0
do you want them all to have cooldown's together or individually? - I'm guessing individually, if so I'd say this way is efficient, I have no clue why attempt one wouldn't work UNLESS the local cooldown interfered with the other functions. shadowstorm440 590 — 4y
0
I want them to have cooldowns individually, Sir. Dehydrocapsaicin 483 — 4y
0
I'd say it's efficient enough, lua is not so easy when it comes to putting in things like if statements or debounces. shadowstorm440 590 — 4y
0
Aight, Sir. Thanks! Dehydrocapsaicin 483 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

You could use a table to store all the cooldowns and the function.

01local attacks = {
02    attack1 = {
03        cooldown = false,
04        run = function()
05            -- Code here
06        end
07    },
08    attack2 = {
09        cooldown = false,
10        run = function()
11            -- Code here
12        end
13    },
14    attack3 = {
15        cooldown = false,
View all 54 lines...

This is something I would do.

Ad

Answer this question