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.

4 comments
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.
I want them to have cooldowns individually, Sir.
I'd say it's efficient enough, lua is not so easy when it comes to putting in things like if statements or debounces.
Aight, Sir. Thanks!