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

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

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

✓ Accepted answer

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!

5 comments

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 ; did you forget to close 'then' at line 12?"

0

Oh yes. Let me add that in.

0

Ok done. It has been fixed.

0

Thanks, it works! I also added in a wait(1) and debounce = false so it can be activated again. Thanks again!

0

Do you by any chance know how to make it not activate when a player touches it?

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