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 add a wait() to this???

i need this to wait 120 seconds before making the noise again

function onTouched(hit)
	script.Parent.Chaching:play()
end
script.Parent.Touched:connect(onTouched)

4 answers

✓ Accepted answer
local touched = false
local waittime = 120 -- seconds to wait

script.Parent.Touched:connect(function(hit)
	if not touched then --if the debounce variable is false, then
		touched = true --make it true!
		script.Parent.Chaching:play() --plays the sound in the brick
		wait(waittime)  --waits what you put as the variables "waittime" until it can be fired again
	end
	touched = false --make it false again so we can do the if statement again after your waittime
end)

1 comment

2

Don't just provide code, explain it. Just providing code without an explanation doesn't let them learn anything.

Basically you would make a debounce.

Debounce in a way disables the script until it needs be used again.

local debounce=false --Making a boolean value to use with debouncing the script
function onTouched(hit)
if debounce==false then -- Checks to see if the debounce is false
debounce=true -- Setting it to true, so if something touches it before the time is up it does nothing
    script.Parent.Chaching:play()
wait(120)
debounce=false -- Setting debounce back to false after 120 seconds so it can be activated again.
end
end
script.Parent.Touched:connect(onTouched)

function onTouched(hit)
	while true do
    script.Parent.Chaching:play()
wait(120)
end
end
script.Parent.Touched:connect(onTouched)

1 comment

0

That's the simple way. All of the rest of you are complicated. Too complicated...

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