Wait timing guarantees

Is this code guaranteed to never error?

local flag=false
coroutine.wrap(function()
	wait(1)
	flag=true
end)()
wait(1)
assert(flag)

Not from what I see. If you are simply setting a boolean to true or false I dont see how its possible to error.

There isn’t a 100% guarantee that flag will be true after the thread’s woken up, since if wait throttles or since it sleeps for around a second, it may not always result in what you’re looking for. It would be better to use something like a BindableEvent (roblox.com) for something like this instead.

local flag = false
local bindable = Instance.new("BindableEvent")
coroutine.wrap(function()
    wait(1)
    flag = true
    bindable:Fire()
end)()
bindable.Event:Wait()
bindable:Destroy()
print("'flag' was set to true")

I hope this helped :slight_smile:

1 Like