Using while loops as modified waits?

Lately I’ve been using while conditional loops as modified waits, instead of event:Wait() due to some parts in my scripts that need to proceed early if a condition is met. However, with the stigma with while loops that I’ve encountered while working with fellow devs, I wonder if this is bad practice.
An example of one of my while loops would be:

while char:IsDescendantOf(game) and char.Stun.Value do
Rservice.Heartbeat:Wait()
end

I personally would just use a repeat check, like so:

repeat wait() until not stun.Value

Edit:
didnt realize wait() was a bad thing to use in loops, so lets modify my original idea, here ya go:

repeat Rservice.Heartbeat:Wait() until not stun.Value
1 Like

Don’t use wait() for loops.

1 Like

Personally I don’t see a problem with what you’re doing. If you’re waiting for certain conditions to be met that are not event-based, a while loop that runs every step / heartbeat is fine.

Oh ok thanks, it is sort of event based however if I were to make it event based I’d have to connect and disconnect many events again and again

What you have already should be ok. There might be ways to force in a fully event based system but that might just add complexity.

For example you could have a system where each independent event sets a flag that the other event(s) has to check.

local event1occured = false
local event2occured = false

local continue = Instance.new("BindableEvent")
continue.Parent = script

local connection1 = SomeEvent1:Connect(function()
	event1occured = true
	if (event2occured) then
		continue:Fire()
	end
end)

local connection2 = SomeEvent2:Connect(function()
	event2occured = true
	if (event1occured) then
		continue:Fire()
	end
end)

print("Waiting for the two events to be fired in any order...")
continue.Event:Wait()
-- cleanup
continue:Destroy()
connection1:Disconnect()
connection2:Disconnect()
print("Both events have been fired!")

As you mentioned earlier although it relies entirely on events it involves connecting and disconnecting multiple events, defines several variables, and even creates a new instance to accomplish something that was already done with 3 lines.