Code after :Wait() firing twice?!

For some reason, :Once() still fires the event twice (printing Unwaiting twice). These prints both happen at the same millisecond so that’s weird. I’ll try to add more print statements to narrow down the issue.

It prints “Waiting” once and “Unwaiting” twice

Maybe this.


local connection
connection = opMenu.Option1.Activated:Connect(function()
    warn("Unwaiting!")
    if changed == true then return end
    changed = false
    chosen = op
    pressed = true
    warn("They chose "..op)
    warn(" ")
    connection:Disconnect() -- Prevents it from firing again
end)

ok that’s strange, if it’s doing that, I have literally no idea

You should maybe use a boolean to keep track of it.

try this, only move the disconnect to the top to reduce chances of microsecond firings

(also maybe consider posting a bug report about it)

1 Like

Or this:


if not spawned then
    spawned = true
    task.spawn(function()
        warn("Waiting...")
        opMenu.Option1.Activated:Wait()
        warn("Unwaiting!")
        if changed == true then return end
        changed = false
        chosen = op
        pressed = true
        warn("They chose "..op)
        warn(" ")
        spawned = false — adjust the flag if needed
    end)
end

Is this in a loop? And what’s the RunContext of the script?

Yes, this is a for loop over a table. This is a module script being required by the client, so I assume that the RunContext is local.

:Wait() only fire once, but it will yield meaning this :Wait() is probably in a loop that havent yet been activated. There is 2 concurrent :Wait() 1 haven’t yet been activated. The other :Wait() activates at the same time because previously it hasn’t yet been activated.

You have to provide more of your code so that we can diagnose the problem, we are only seeing half the picture.

The RunContext is whatever the RunContext property of the script requiring the module is. If it’s set to client (not a LocalScript) it will run in StarterPlayerScripts and PlayerScripts instead of only in PlayerScripts.