While wait/true() VS PropertyChanged?

I’ve heard a lot of things going around that while wait() is inefficient etc etc, that it should never be used under any circumstance.

Is it really worth the hassle to use propertychanged over while wait/true

For example: A text box showing how much stats you have on a gui that updates, should it use property changed and then update orrr?

Thank you I just want some clarity on the subject as it’s not clear either way.

1 Like

if it yields, you game lag so dont use wait() especially in loops
instead of wait()
you can use events

game:GetService("RunService").Heartbeat:Wait()

just use changed event, using while loops just waste ur lag performance
–setting the text to the same value and etc

3 Likes

I used GetPropertyChangedSignal() for this kind of thing because from what I know While loop uses a memory everytime it loops. I dunno if Im right.

Yes it is way better to use an event to change some value in a UI rather than polling all the time in a while loop if that is why you ask.

However there are situations where waiting on an event is hard to do (programatically) so you might opt for while loops. An example is when you want to check when a player walked far enough from an npc. This cannot be detected usually with events.

In your situation 100% try to do it with some event instead of while. Actually always try event and only if it doesn’t work go for while polling.

EDIT: Just to reply to @BasedKnowledge. That is even worse than doing wait(). Because Heartbeat:Wait() runs at every server step. This means that it runs by definition at least as frequent as wait() but sometimes it will run even more times. Thus, you waste even more processing in your idle wait loop.

1 Like

It is more of a hassle to do the latter.

You only want to update the text box whenver your stats change. Not every 1/30th of a second.

Does yield mean when the loop is running but not actually doing something, and thank you all for the replies it now adds up :slight_smile:

Unnecessary loops cause lag, as well as yielding (which stops code after it running until the loop is over), so you’d need coroutines or something.

A generally simple rule in most cases: Events > Loops

1 Like

Yielding means that the thread does NOT run anymore. In event based programming you want your threads to yield when there is not work to be done. You wake the threads up when there is an event only. In the case of Roblox if you set a listener with Connect(), that spawns a thread every time that event is triggered. So that is the best way to change the values in your UI.

1 Like

Telling someone to not use wait and then telling them to use events but suggesting a replacement to wait is just bad practice with extra steps. You’re doing the same thing if not worse with Heartbeat:Wait().

Also @Sorbious that’s a weird way of phrasing it; I wouldn’t give that the right answer it’s more-so unneeded computations.

1 Like

i am very sorry, i just follow this old toturial it is very good and never had lag until now becuase of it