Which of these methods for wait() is more efficient?

Hey guys, just a quick question.

I was having trouble with a line of code not executing properly, as the property change happens before it ever reaches the wait().

Code I had:

gameOver.Value = GameFunctions.checkForWinner()
gameOver:GetPropertyChangedSignal("Value"):wait()

Code I replaced it with:

gameOver.Value = GameFunctions.checkForWinner()
repeat wait() until gameOver.Value == true

Is using a repeat until going to cause lag or other unwanted problems in my game? What are the benefits/detriments to either of these methods?

Thanks!

1 Like

I don’t think it will hinder your performance too much. As long as that line of code isn’t used many times per minute / second you should be good to go. I have certain inefficient parts of code in the games that I work on, these are for cases in which it wouldn’t make sense to optimize since it’s only running once every 5 mins.

1 Like

Never use a repeat loop to check for a change. It is very unefficient as wait may take more than the specified time. Please use GetPropertyChangedSignal:Wait()

1 Like

The problem I’m having with the GetPropertyChangedSignal:Wait() is that it continues to wait after the value has changed. I’m not sure if this is because I am calling a function between the value changing and reaching the line where it checks if it has changed.

The fault is in your own logic that you specified in your script.

1 Like

The first code would be used for physical variables (values and properties) And the second for variables in scripts (debounce), although the second could be more secure in some cases (maybe as verification)

1 Like

Value objects have a dedicated Changed event, using GetPropertyChangedSignal is just not correct for this property. Also, your two code variations are not equivalent code. Both check the value, but one (once fixed) would wait for it to be true, while the other just waits for any change.

1 Like

Try

if not gameOver.Value then
    gameOver:GetPropertyChangedSignal("Value"):Wait()
end

It only yields if the game isn’t over by the time the code runs, which is exactly what your busy poll loop does without wasting CPU time.

2 Likes