Alt to wait() function

What other alternatives are there to wait()? I have a script constantly checking a condition and I really don’t want to waste memory by using

while true do 
    if this == that then
        ...
    end
    wait(1)
end

Is there a way to meet the condition without using this method?

If you are checking if this is equal to that in a loop, most likely something must’ve been changed. So I would use the .Changed event to check so it is not constantly running. Then you can add your if statement to check and continue with the rest of your code.

There are several listeners you could use so that you don’t have to constantly check/wait for a condition to be true. For instance, GUI buttons have an Activated listener so that you can run code right when the user clicks the button, rather than having to constantly check whether the user’s mouse is inside the button.

Example:

guiButton.Activated:Connect(function()
    --runs code as soon as user clicks the button
end)

I just have one question: what condition are you trying to meet?

How do I use .Changed event?

It depends on the case, but generally this == that means an arbtruary case because I always knew using while true loops are bad so this is just a general question.

Scripts don’t really take up too much memory, no worries about that. But you really need to learn the good habit of using the conditional statement:

while this == that do 
    wait(1)
end

Alternatives to waits can be Hearbeat, Renderstepped, or Stepped while using Runservice

local RS = game:GetService("RunService")

RS.Heatbeat:Wait()
RS.RenderStepped:Wait()
RS.Stepped:Wait()

As long as the condition is true it will keep running.

When the script runs and this ~= that, will the script ever return back to that condition?

The .Changed event can be used in many ways. For example if you had

function onChanged()
  if this == that then
     --Code here
   end
end

game.Players.LocalPlayer.leaderstats.Coins.Changed:Connect(onChanged)

That would fire every time a leaderstats in the coins part was changed.

By Changing does that mean the Instance was updated to any other value than before?

Changed event basically detects the change of one of the properties of instance

It’ll go off of any change to the stats. So if you reset the number to a specific number it currently is, it should fire the change.

If you just want to wait until two variables are equal, your method is fine.