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.
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.