How can I input a cooldown in my Whistling script?

I want to prevent players from spamming the whistle key bind.

local Player = script.Parent


local function playSound()
	local sound = Instance.new("Sound")
	sound.SoundId = "rbxassetid://9120675904"
	sound.Parent = game.Workspace
	sound:Play()
end

game:GetService("UserInputService").InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.T then
		playSound()
	end
end)


You can use a Sound.Ended:Wait() event + With some debouncing, I can give you a little showcase if you want.

1 Like
local Player = script.Parent


local function playSound()
	local sound = Instance.new("Sound")
	sound.SoundId = "rbxassetid://9120675904"
	sound.Parent = game.Workspace
	sound:Play()
end

local onCooldown = false
local cooldownTime = 2
game:GetService("UserInputService").InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.T and not onCooldown then
		onCooldown = true
		playSound()
		task.delay(cooldownTime, function()
			onCooldown = false
		end)	
	end
end)
1 Like

Thats what debouncing looks like OP, but make sure to also add a sound.Ended:Wait() event instead of using the cooldowntime thing that fusionet has wrote. As it’ll be accurate.

1 Like

i would check for the cooldown first and then the keybind

if not onCooldown and input.KeyCode == Enum.KeyCode.T then

“if you’re on cooldown then why would the keycode matter” - my thinking

i wonder if This even matters

1 Like

Probably not but thanks bro. :skull_and_crossbones:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.