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

How do I put a wait() in a if statement that has debounce in it?

How would I fix this so it doesn't spam the audio and brightness, but keeps the wait timers in the code without removing debounce?

local shortcut = game.Workspace.Map
local lighting = shortcut.Lighting
local flicker = lighting.Ceiling_light.Lightbulb.SpotLight
local audio = lighting.Ceiling_light.Lightbulb["Lightbulb Flicker"]
local audioo = lighting.Ceiling_light.Lightbulb.zap
local debounce = false

script.Parent.Touched:Connect(function()
if debounce == false then
	debounce = true
	if script.Parent.Touched then
	audio:play()
	flicker.Brightness = 0
	wait(1)
	flicker.Brightness = 15
	audioo:play()
	wait(1)
	flicker.Brightness = 0
	wait(1)
	flicker.Brightness = 15
		end
	debounce = false
end
	if script.Parent.TouchEnded then
	end
script.Parent:Destroy()
end)

1 answer

✓ Accepted answer

wait() must be put before the debounce changes value. So your code will be this:


local shortcut = game.Workspace.Map
local lighting = shortcut.Lighting
local flicker = lighting.Ceiling_light.Lightbulb.SpotLight
local audio = lighting.Ceiling_light.Lightbulb["Lightbulb Flicker"]
local audioo = lighting.Ceiling_light.Lightbulb.zap
local debounce = false

script.Parent.Touched:Connect(function()
    if debounce == false then
        debounce = true
        audio:Play()
        flicker.Brightness = 0
        wait(1)
        flicker.Brightness = 15
        audioo:Play()
        wait(1)
        flicker.Brightness = 0
        wait(1)
        flicker.Brightness = 15
        wait(5) -- Replace 5 to how many seconds you want
        debounce = false
    end
end)

script.Parent.TouchEnded:Connect(function()
    script.Parent:Destroy()
end)

Hope it helped, I fixed your code and I put the wait in line 20. If you may encounter problems, comment.

2 comments

0

It fixed my problem, thank you.

0

No problem, glad I've helped.

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