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

How do i make a script to play a sound once?

Asked by 6 years ago
Edited 6 years ago

i am trying to make it when a player touches a part it played a sound once, and the sound can't be played again until they leave it. and if they touch it again, it will play. This is what i thought of, and it doesn't seem to work. Any suggestions?

local debounce = false

script.Parent.Touched:connect(function(hit1)
    if debounce == false then
        script.Parent.Sound:Play()
        debounce = true
    end
end)

script.Parent.TouchEnded:connect(function(hit2)

debounce = false

end)

i do not want the sound to be played locally to the player.

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

If you don't want to sound to be played locally to the player, then use a ServerScript.

local debounce = false

script.Parent.Touched:connect(function(hit1)
    if not debounce then
    debounce = true
        script.Parent.Sound:Play()
        wait(3) --how long you want before it can be touched and played again
    debounce = false
    end
end)

Let me just run through quickly on what you did wrong.

local debounce = false

script.Parent.Touched:connect(function(hit1)
    if debounce == false then
        script.Parent.Sound:Play()
        debounce = true 
    end
end)

Here, you placed debounce = true and debounce = false in the wrong place.

Btw, idk why you put hit1 in the parenthesis. Unless you're trying to identify that it's a player, I suggest that you do this:

local debounce = false

script.Parent.Touched:connect(function(hit1)
local character = hit1.Parent
    if not debounce then
    debounce = true
    if character and character:findFirstChild("Humanoid") then
        script.Parent.Sound:Play()
    end
        wait(3) --how long you want before it can be touched and played again
    debounce = false
    end
end)

What that does is that it looks for "Humanoid" in the character.

Hope this helps!

EDIT(because asked):

local debounce = false

script.Parent.Touched:connect(function(hit1)
local character = hit1.Parent
    if not debounce then
    debounce = true
    if character and character:findFirstChild("Humanoid") then
        script.Parent.Sound:Play()
    end
        wait(3) --how long you want before it can be touched and played again
    debounce = false
    end
end)

script.Parent.TouchEnded:connect(function()
    --your code
end)
0
how do i make it so at line 10 it has to wait until the player is not touching the part? Creeper_Dude158 11 — 6y
0
added an extra part because you asked, try that GIassWindows 141 — 6y
0
added an extra part because you asked, try that! GIassWindows 141 — 6y
0
had to manipulate a bit, but eventually worked. thanks! i found out the part that they had to touch first was too small. so i had to make another one that is bigger Creeper_Dude158 11 — 6y
Ad

Answer this question