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 3 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:

local UIS = game:GetService("UserInputService")

local cooldown = false

local function attack1()

    if not cooldown then
        cooldown = true
        --block of codes
        wait()
        cooldown = false
    end

end 

local function attack2()

    if not cooldown then
        cooldown = true
        --block of codes
        wait()
        cooldown = false
    end

end 

local function attack3()

    if not cooldown then
        cooldown = true
        --block of codes
        wait()
        cooldown = false
    end

end 

local function attack4()

    if not cooldown then
        cooldown = true
        --block of codes
        wait()
        cooldown = false
    end

end 

UIS.InputBegan:Connect(function(input, gameProcessed)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        if input.KeyCode == Enum.KeyCode.Q then
            attack1()
        end

        if input.KeyCode == Enum.KeyCode.E then
            attack2()
        end

        if input.KeyCode == Enum.KeyCode.Z then
            attack3()
        end

        if input.KeyCode == Enum.KeyCode.C then
            attack4()
        end
    end
end

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

local UIS = game:GetService("UserInputService")

local function attack1()
    local cooldown = false

    if not cooldown then
        cooldown = true
        --block of codes
        wait()
        cooldown = false
    end

end 

local function attack2()
    local cooldown = false

    if not cooldown then
        cooldown = true
        --block of codes
        wait()
        cooldown = false
    end

end 

local function attack3()
    local cooldown = false

    if not cooldown then
        cooldown = true
        --block of codes
        wait()
        cooldown = false
    end

end 

local function attack4()
    local cooldown = false

    if not cooldown then
        cooldown = true
        --block of codes
        wait()
        cooldown = false
    end

end 

UIS.InputBegan:Connect(function(input, gameProcessed)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        if input.KeyCode == Enum.KeyCode.Q then
            attack1()
        end

        if input.KeyCode == Enum.KeyCode.E then
            attack2()
        end

        if input.KeyCode == Enum.KeyCode.Z then
            attack3()
        end

        if input.KeyCode == Enum.KeyCode.C then
            attack4()
        end
    end
end

Attempt No. 2

local UIS = game:GetService("UserInputService")

local cooldown = false
local cooldown2 = false
local cooldown3 = false
local cooldown4 = false

local function attack1()

    if not cooldown then
        cooldown = true
        --block of codes
        wait()
        cooldown = false
    end

end 

local function attack2()

    if not cooldown2 then
        cooldown2 = true
        --block of codes
        wait()
        cooldown2 = false
    end

end 

local function attack3()

    if not cooldown3 then
        cooldown3 = true
        --block of codes
        wait()
        cooldown3 = false
    end

end 

local function attack4()

    if not cooldown4 then
        cooldown4 = true
        --block of codes
        wait()
        cooldown4 = false
    end

end 

UIS.InputBegan:Connect(function(input, gameProcessed)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        if input.KeyCode == Enum.KeyCode.Q then
            attack1()
        end

        if input.KeyCode == Enum.KeyCode.E then
            attack2()
        end

        if input.KeyCode == Enum.KeyCode.Z then
            attack3()
        end

        if input.KeyCode == Enum.KeyCode.C then
            attack4()
        end
    end
end

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 — 3y
0
I want them to have cooldowns individually, Sir. Dehydrocapsaicin 483 — 3y
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 — 3y
0
Aight, Sir. Thanks! Dehydrocapsaicin 483 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

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

local attacks = {
    attack1 = {
        cooldown = false,
        run = function()
            -- Code here
        end
    },
    attack2 = {
        cooldown = false,
        run = function()
            -- Code here
        end
    },
    attack3 = {
        cooldown = false,
        run = function()
            -- Code here
        end
    },
    attack4 = {
        cooldown = false,
        run = function()
            -- Code here
        end
    }
}

local function attack(name)
    if not attacks[name].cooldown then
        attacks[name].cooldown = true
        attacks[name].run()
        attacks[name].cooldown = false
    end
end

game:GetService("UserInputService").InputBegan:Connect(function(input, gameProcessed)
    if input.UserInputType == Enum.UserInputType.Keyboard then
        if input.KeyCode == Enum.KeyCode.Q then
            attack("attack1")
        end

        if input.KeyCode == Enum.KeyCode.E then
            attack("attack2")
        end

        if input.KeyCode == Enum.KeyCode.Z then
            attack("attack3")
        end

        if input.KeyCode == Enum.KeyCode.C then
            attack("attack4")
        end
    end
end

This is something I would do.

Ad

Answer this question