Debounce for ContextActionService

So I am playing around with ContextActionService and I wanted to add a delay, it works after I press K and it waits for 1 second. However, if I spam K, the debounce gets broken and doesnt wait at all. What should I do? And if my code has any room for improvements/more efficient ways to do, please tell me! I am trying to be better than yesterday.

local CAS = game:GetService("ContextActionService")

local map = script.Parent:FindFirstChild("main_map")
local inventory = script.Parent:FindFirstChild("main_inventory")

local function toggleMap(inputName, inputState, inputObject)
	if inputState == Enum.UserInputState.Begin then
		map.Enabled = true
	else
		map.Enabled = false
	end
end

local db = true
local function toggleInventory(inputName, inputState, inputObject)
	if inputState == Enum.UserInputState.Begin and db == true then
		db = false
		inventory.Enabled = true
	else
		inventory.Enabled = false
	end
	task.wait(1)
	db = true
end

--binds
CAS:BindAction("toggleMap", toggleMap, true, Enum.KeyCode.M)
CAS:BindAction("toggleInventory", toggleInventory, true, Enum.KeyCode.K)
1 Like