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

Cooldown for part.Touched:connect()?

Hello y'all,

I'll start off by showing what my code is at the moment:

Shrek = game.Workspace.Shrek.Head
Allstarsound = game.Workspace.Allstar
ShrekTouched = Shrek.Touched
function Allstar()
	Allstarsound:Play()
	wait (59)
	Allstarsound:Stop()
end

Shrek.Touched:connect(Allstar)

I want the "Shrek.Touched:connect(Allstar)" part to have a cooldown, so the sound wont start again when you start it. I tried the following code:

if Shrek.Touched then
	Allstar()
	wait (75)
end

Which ended me up with the sound to be played when joined.

If you have any questions, feel free to ask me.

1 answer

You can use a cooldown debounce.

Shrek = game.Workspace.Shrek.Head
Allstarsound = game.Workspace.Allstar

local isInCooldown = false
local cooldownTime = 75

function Allstar()
	if isInCooldown then return end
	Allstarsound:Play()
	wait (59)
	Allstarsound:Stop()
	isInCooldown = true
	wait(cooldownTime)
	isInCooldown = false
end

Shrek.Touched:connect(Allstar)

2 comments

0

Worked after I did some adjustments, because it would play the song, wait 59 seconds and THEN do the cooldown which ended up to not work correctly. Got it now, thanks anyways!

0

Ohh. lol. I didn't even see you had the wait(50)

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