I have a scripted a part to glow and blink when touched, and want a bing sound to come out(also done that)
Problem = The function is repeated more than 1 time in a second
For e.g if you go run over the part, it will produce more than 1 bing sounds and will repeat the function until all parts are off the part. Now I only want the function to be repeated once per 5 seconds and not just spam when ur just standing or moving on it.
local t = 0.15 local s = script.Parent s.Transparency = 0.5 s.Touched:connect(function(lol) script.Sound:Play() wait(t) s.Transparency = 0.25 s.BrickColor = BrickColor.new("Moss") wait(t) s.BrickColor = BrickColor.new("Pastel green") wait(t) s.BrickColor = BrickColor.new("Moss") wait(t) s.Transparency = 0.5 end)
Take sound Id as 241852639 and please help
here
local db = 0 -- sets variable local function OnTouched () if db == 0 then -- if db is 0 then db = db + 1 -- sets db to 1 script.Sound:Play() wait (0.15) script.Parent.Transparency = 0.25 script.Parent.BrickColor = BrickColor.new("Moss") wait(0.15) script.Parent.BrickColor = BrickColor.new("Pastel green") wait(0.15) script.Parent.BrickColor = BrickColor.new("Moss") script.Parent.Transparency = 0.5 wait() -- set this to how much time before you can touch it again db = db - 1 else return end -- if db is not 0 then it ends end script.Parent.Touched:Connect(OnTouched)
What you need here is a debounce
WHAT IS A DEBOUNCE?
On simpler terms, it essentially allows or disallows code that you only want ran once during a specific amount of time. Debounces is just a variable that bounces between two values with a conditional statement to see if your code is ready to be ran or not.
Debounces can be any value type that has at least two separate values. So number, strings or bools can be used. Though for simplicity, bools would be best since all you need are two values.
An example:
local debounce = false -- Original value workspace.Part.Touched:Connect(function(hit) -- The touched event will fire many times... if debounce == false then -- Unless you have a debounce! debounce = true -- "Bounce" to the other value --[[Since the event fires multiple times, the debounce will prevents your code being ran again before this event finishes --]] -- STUFF YOU WANT DONE HERE wait(1) -- You MUST have a wait, 1 second should suffice debounce = false -- "Bounce" back to the original value end end)
If there are any other questions, feel free to ask!