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

[SOLVED] How do I get a sound to play once when a tool touches it?

This is solved

I am a VERY newbie scripter (I can't really write any code myself without a reference, but slightly better with editing pre-made scripts) and I am trying to make a keycard door with a sound that plays when it opens and closes. It probably has something to do with the fact it's not all made for how I changed it, but it plays multiple times when it opens/closes if I don't instantly take away the tool from touching it. It mostly ends up going 2 or 3 times. (Hope you understand) How do I fix this? This is what I have: (I HAVE NO IDEA HOW TO MAKE THE CODE IN THE SPECIAL BLOCK THING, IF IT GETS TAKEN DOWN DUE TO THAT PLEASE TELL ME HOW :p)

script.Parent.Touched:Connect(function(hit) if hit.Parent.Name == "Access Armory" then script.Parent.Parent.Door1.Part1.Transparency = 1 script.Parent.Parent.Door1.Part1.CanCollide = false script.Parent.Parent.Door1.Part2.Transparency = 1 script.Parent.Parent.Door1.Part2.CanCollide = false script.Parent.Parent.Door2.Part1.Transparency = 0 script.Parent.Parent.Door2.Part1.CanCollide = true script.Parent.Parent.Door2.Part2.Transparency = 0 script.Parent.Parent.Door2.Part2.CanCollide = true script.Parent.Parent.Door2.Part3.Transparency = 0 script.Parent.Parent.Door2.Part3.CanCollide = true script.Parent.Parent.Door2.Part4.Transparency = 0 script.Parent.Parent.Door2.Part4.CanCollide = true script.Parent.Open:play() wait(3) script.Parent.Parent.Door1.Part1.Transparency = 0 script.Parent.Parent.Door1.Part1.CanCollide = true script.Parent.Parent.Door1.Part2.Transparency = 0 script.Parent.Parent.Door1.Part2.CanCollide = true script.Parent.Parent.Door2.Part1.Transparency = 1 script.Parent.Parent.Door2.Part1.CanCollide = false script.Parent.Parent.Door2.Part2.Transparency = 1 script.Parent.Parent.Door2.Part2.CanCollide = false script.Parent.Parent.Door2.Part3.Transparency = 1 script.Parent.Parent.Door2.Part3.CanCollide = false script.Parent.Parent.Door2.Part4.Transparency = 1 script.Parent.Parent.Door2.Part4.CanCollide = false script.Parent.Close:play() end

end)

1 answer

You can play a sound using the Play function. To make it so it doesn't play multiple times, add a debounce. Example:

local Audio = workspace:WaitForChild('CoolAudio') -- Change this to where your sound is
local debounce = false -- our debounce

function T(h)
if debounce == true then
return -- If debounce is true we will return/end the function
end
debounce = true -- Activate the debounce
Audio:Play() -- The function
wait(3) -- Wait
debounce = false -- Change the debounce back to false
end

Full edited script:

local debounce = false
script.Parent.Touched:Connect(function(hit) 
if hit.Parent.Name == "Access Armory" and debounce == false then -- Also if debounce is false, if not it wont do anything
debounce = true
script.Parent.Parent.Door1.Part1.Transparency = 1 script.Parent.Parent.Door1.Part1.CanCollide = false script.Parent.Parent.Door1.Part2.Transparency = 1 script.Parent.Parent.Door1.Part2.CanCollide = false script.Parent.Parent.Door2.Part1.Transparency = 0 script.Parent.Parent.Door2.Part1.CanCollide = true script.Parent.Parent.Door2.Part2.Transparency = 0 script.Parent.Parent.Door2.Part2.CanCollide = true script.Parent.Parent.Door2.Part3.Transparency = 0 script.Parent.Parent.Door2.Part3.CanCollide = true script.Parent.Parent.Door2.Part4.Transparency = 0 script.Parent.Parent.Door2.Part4.CanCollide = true script.Parent.Open:play() wait(3) script.Parent.Parent.Door1.Part1.Transparency = 0 script.Parent.Parent.Door1.Part1.CanCollide = true script.Parent.Parent.Door1.Part2.Transparency = 0 script.Parent.Parent.Door1.Part2.CanCollide = true script.Parent.Parent.Door2.Part1.Transparency = 1 script.Parent.Parent.Door2.Part1.CanCollide = false script.Parent.Parent.Door2.Part2.Transparency = 1 script.Parent.Parent.Door2.Part2.CanCollide = false script.Parent.Parent.Door2.Part3.Transparency = 1 script.Parent.Parent.Door2.Part3.CanCollide = false script.Parent.Parent.Door2.Part4.Transparency = 1 script.Parent.Parent.Door2.Part4.CanCollide = false script.Parent.Close:play()
debounce = false
 end

end)

Hope this helps!

10 comments

0

I have the audio and it plays, it just plays multiple times and I want it to play once

0

You need to add a debounce then. Ill edit my answer. edited

0

I tried adding it, and it completely disables the door. this happens with a lot of things I try to ad to that script. So, since you probably don't want to tell me how to entirely make a keycard door, I don't think it will work, and I don't think that's allowed since you can't make requests. Thanks though, I will keep it in mind

0

I have nothing better to do; I just edited your script.

0

wow. thank you, i feel bad for doing nothing but having someone else do it.

0

You're welcome, please mark it as answered

0

Funny this is, I have no idea how. I literally just created my account to ask this. I looked around, couldn't find it

0

It should be under my answer, called 'Mark as Answered'. I've never answered a question yet, so I'm not sure either.

0

Doesn't show up, I might just delete it, since that works.

0

dont delete it, or put SOLVED in it.

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