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

Where do I insert debounce into my script so the part wont trigger the function multiple times?

Asked by 4 years ago

Lets say I have 2 parts. Front, and FrontDamaged. My script makes Front transparent, and FrontDamaged not transparent, whenever Front touches another part. It also plays a sound when it touches something. The thing is, it has no cooldown. And im stuck on where to add the debounce at.

01local Front = script.Parent.Front
02local FrontDamaged = script.Parent.FrontDamaged
03local sound = script.Parent.Crash
04local debounce = false
05 
06 
07if not sound.IsLoaded then
08    sound.Loaded:wait()
09end
10 
11local function onPartTouched(otherPart)
12    print(Front.Name .. " has touched " .. otherPart.Name)
13    Front.Transparency = 1
14    FrontDamaged.Transparency = 0
15    sound:Play()
16 
17end
18 
19Front.Touched:Connect(onPartTouched)

TLDR: I need help putting a debounce so the part wont touch stuff multiple times at once.

1 answer

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

I put comments where you add it.

01local Front = script.Parent.Front
02local FrontDamaged = script.Parent.FrontDamaged
03local sound = script.Parent.Crash
04local debounce = false
05 
06 
07if not sound.IsLoaded then
08    sound.Loaded:wait()
09end
10 
11local function onPartTouched(otherPart)
12if debounce == false then --Here we check if it has been activated.
13    print(Front.Name .. " has touched " .. otherPart.Name)
14    Front.Transparency = 1
15    FrontDamaged.Transparency = 0
View all 21 lines...

Let me know if it worked!

0
Front.Touched:Connect(onPartTouched) is underlined red, and i have an error in output that says "18:12:48.076 - Workspace.mcslimemansCar.Crash Script:21: Expected 'end' (to close 'function' at line 11), got <eof>; did you forget to close 'then' at line 12?" mcslimeman 37 — 4y
0
Oh yes. Let me add that in. JailBreaker_13 350 — 4y
0
Ok done. It has been fixed. JailBreaker_13 350 — 4y
0
Thanks, it works! I also added in a wait(1) and debounce = false so it can be activated again. Thanks again! mcslimeman 37 — 4y
0
Do you by any chance know how to make it not activate when a player touches it? mcslimeman 37 — 4y
Ad

Answer this question