Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make this Touched event delayed?

Asked by 6 years ago

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

Log in to vote
0
Answered by 6 years ago

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)
0
thank you for the detailed response and explanation, very helpful :) DannyKyun 14 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

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)

Answer this question