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

How to make this Touched event delayed?

For this script, touching a specific brick here would play a noise, however I've noticed that you can touch this brick repeatedly and it will simply repeat the soundid from the very beginning which is very janky and abusable.

script.Parent.Touched:connect(function(part)
    local plr = game.Players:GetPlayerFromCharacter(part.Parent)

    if plr then
        script.Parent.Sound:Play()
    end

	end)

I was thinking of simply putting a delay in the script using wait() after the first use but the script doesn't seem to work

script.Parent.Touched:connect(function(part)
    local plr = game.Players:GetPlayerFromCharacter(part.Parent)

    if plr then
        script.Parent.Sound:Play()

local play = script.Parent.Sound:Play()
if play then wait(15)
	
end
    end

	end

Help would be much appreciated!

2 answers

✓ Accepted answer

###To accomplish this, use a debounce. Also, the Sound:Play() method returns void, so there is no point of storing it in a variable.

local debounce = false -- will be set to true once enabled

script.Parent.Touched:Connect(function(part) -- connect is deprecated, use Connect
    local plr = game.Players:GetPlayerFromCharacter(part.Parent)

    if plr and not debounce then -- if it's a player and debounce is enabled 
        debounce = true -- set to true first, then run code 
        script.Parent.Sound:Play()
        wait(15) -- wait time between activations 
        debounce = false -- set to false again, the code can repeat. if you do not want this, remove this line
    end
end)

1 comment

0

thank you for the detailed response and explanation, very helpful :)

First, i don't understand why you put it AFTER all the scripts, and second "local play...." was not needed. Here's the solved code

script.Parent.Touched:connect(function(part)
    local plr = game.Players:GetPlayerFromCharacter(part.Parent)

    if plr then
	    wait(15)
            script.Parent.Sound:Play()
    end
end)

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