Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to put debounces to multiple functions efficiently?

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

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.

0

I want them to have cooldowns individually, Sir.

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.

0

Aight, Sir. Thanks!

1 answer

✓ Accepted answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.