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

How do I add a cooldown to the sound in my code?

For my horror game, I've made a moving door with a sound that is connected with a button, but the sound plays again every time you click the button with no cooldown. I have tried many ways to add a cooldown but none of them work for me.

local ClickDetector = script.Parent.ClickDetector

local Door = script.Parent.Parent.Door

local TweenService = game:GetService("TweenService")

local Info = TweenInfo.new(
	
	2,
	Enum.EasingStyle.Back,
	Enum.EasingDirection.Out,
	0,
	true,
	0
)			

local Goal =
	
    {
		Position = Vector3.new(Door.Position.X - 4, Door.Position.Y - 0.5,Door.Position.Z - 1.7)
	}

local MoveDoor = TweenService:Create(Door, Info, Goal)

ClickDetector.MouseClick:Connect(function()

	MoveDoor:Play()
	script.RustyDoor:Play()
	
end)
		

1 answer

✓ Accepted answer

You should use debug/ debounces.

Also, debounces does not add a cool-down to your script, you need to add a wait inside:

Example:

debounce = false

script.Parent.Touched:Connect(function(player)
	if debounce == false then
		debounce = true
		print(player.Parent.Name .." touched me!"
		wait(1)
		debounce = false
	else return end
end)

This will not spam the message "GamingMadhi touched me!", but waits a second and detects if your touching and launch again.

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