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.

local Front = script.Parent.Front
local FrontDamaged = script.Parent.FrontDamaged
local sound = script.Parent.Crash
local debounce = false


if not sound.IsLoaded then
    sound.Loaded:wait()
end

local function onPartTouched(otherPart)
    print(Front.Name .. " has touched " .. otherPart.Name)
    Front.Transparency = 1
    FrontDamaged.Transparency = 0
    sound:Play()

end

Front.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.

local Front = script.Parent.Front
local FrontDamaged = script.Parent.FrontDamaged
local sound = script.Parent.Crash
local debounce = false


if not sound.IsLoaded then
    sound.Loaded:wait()
end

local function onPartTouched(otherPart)
if debounce == false then --Here we check if it has been activated. 
    print(Front.Name .. " has touched " .. otherPart.Name)
    Front.Transparency = 1
    FrontDamaged.Transparency = 0
    sound:Play()
debounce = true --We set it to true so it can't be activated again. 
end
end

Front.Touched:Connect(onPartTouched)

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