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

Function seems to be repeating more than one time in 5 seconds?

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

1 comment

0

You should probably also make sure the part touching is a player part.

2 answers

✓ Accepted answer

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)

6 comments

0

what is db

0

its debounce just like precise logic said

0

oh okay thx

0

how does the script or the game know that db is debounce, can you type anything instead of debounce?

0

yes

0

it does't need to be "debounce" it can be anything as long it has a "value"

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!

1 comment

0

should the stuff i want to be done be inbetween the debounce command or should it be outside the debounce command

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