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

How would I make a cooldown system for this?

So basically on my end, while testing the PC and XBOX controls. The PC controls work perfectly fine but when pushing the button on the other hand the function likes to repeat which has left me confused so I've thought about using a wait() and a while loop that triggers a bool value I just don't know how to execute it.

local player = game.Players.LocalPlayer
local inputServ = game:GetService("UserInputService")
local Key = Enum.KeyCode.E
local Button = Enum.KeyCode.ButtonX


inputServ.InputBegan:connect(function(input)
	if input.KeyCode == Key then
		print("PC Pressed")
		script.Activated:FireServer()
	end
inputServ.InputBegan:connect(function(input)
	if input.KeyCode == Button then
		print("XBOX Pressed")
		script.Activated:FireServer()
	end
end)
end)

1 answer

✓ Accepted answer

You need to use debounce, so here is an example:

local UIS = game:GetService("UserInputService")
local ready = true

UIS.InputBegan:Connect(function(input)
	if ready == true then
		ready = false
		if input.UserInputType == Enum.UserInputType.Keyboard then --Checking if player inputed from keyboard or some other device
			if input.KeyCode == Enum.KeyCode.X then --Checking keycode
				print("Player pressed X using keyboard") --printing
				wait(.5) --Cooldown time
				ready = true
			end
		elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
			if input.KeyCode == Enum.KeyCode.ButtonX then
				print("player pressed X using gamepad")
				wait(.5)
				ready = true
			end
		end
	end
end)

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