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

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

Asked by 3 years ago
Edited 3 years ago

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

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

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.

0
It fixed my problem, thank you. Zeta_Dev 34 — 3y
0
No problem, glad I've helped. Gabe_elvin1226aclan 323 — 3y
Ad

Answer this question