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

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

Asked by 6 years ago

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

0
You should probably also make sure the part touching is a player part. Meltdown81 309 — 6y

2 answers

Log in to vote
0
Answered by
CjayPlyz 643 Moderation Voter
6 years ago
Edited 6 years ago

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)
0
what is db AlexStorms 21 — 6y
0
its debounce just like precise logic said CjayPlyz 643 — 6y
0
oh okay thx AlexStorms 21 — 6y
0
how does the script or the game know that db is debounce, can you type anything instead of debounce? AlexStorms 21 — 5y
View all comments (2 more)
0
yes CjayPlyz 643 — 5y
0
it does't need to be "debounce" it can be anything as long it has a "value" CjayPlyz 643 — 5y
Ad
Log in to vote
0
Answered by 6 years ago

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!

0
should the stuff i want to be done be inbetween the debounce command or should it be outside the debounce command AlexStorms 21 — 6y

Answer this question