How to use :Wait() for when a value reaches a number?

I want to call a function once a value has reached the number 500, and wanted to use “wait() until” to do this, but i heard using :Wait() is better in this scenario.
How exactly can I use :Wait() in this context?

1 Like

Do you mean something like this?

local num = 0

local function print500()
	print("500!!!")
end

repeat
	num += 1
	game:GetService("RunService").Heartbeat:Wait()
until num == 500

print500()

Not sure if I understood correctly

1 Like

Sorry, I should’ve been more specific, what I meant is how to check if the number has reached 500 from an extern script, so one script would do the counting while the other one is waiting for it to reach a number.

Do you mean waiting for an IntValue to contain 500 for example? If so, you can use the Changed event instead

1 Like

But doesn’t Changed only work for physical Values? What if I would have a value saved inside a module script dictionary for example?

:Wait() is an alternative to :Connect() for event signals, just do :GetPropertyChangedSignal(“Value”) or .Changed and check if the number is 500. If it’s a variable in your script, check if the number is 500 every time you update the number, if it is then call the function. If you want a new thread make a bindable event.

1 Like

Yes you can do that actually since I did a quick test, I had 1 script with this

local module = require(game.ServerStorage.ModuleScript)

while true do
	print(module.Banana)
	wait(1)
end

And another with this

local module = require(game.ServerStorage.ModuleScript)

while true do
	wait(1)
	module.Banana += 1
end

image

And the one what prints was correctly identifying the updated value. So you could use it for Module script dictionaries as well. You just need a loop to go until that value is 500

You can use a signal class, with the object-oriented approach

local Signal = require(path.to.signal)

local Number = {}
Number.__index = Number

function Number.new(value)
    return setmetatable({Reached500 = Signal.new(), Value = value}, Number)
end

function Number:Increment(value)
    self.Value += value
    if self.Value >= 500 then
        self.Reached500:Fire()
    end
end

local num = Number.new(20)
num:Increment(500)
num.Reached500:Wait()

You can find a signal class somewhere on github, or devforum

3 Likes

Or you can just make a BindableEvent. No need to reinvent the wheel here.

7 Likes

Agreed, for cross-script events, a signal class is not the way to go. Normally, they work best in ModuleScripts or in single-script scenarios. BindableEvent events, although they deepcopy the arguments (not big of a deal because they are just a number), it will work better and be easier to use.

You can try it like this:

if (number/yourVariable) >= 500 then
wait()
end)

That will wait for a sixtieth of a second, and then continue even if it’s not greater than 500.

1 Like

I think you should tell is what you’re actually trying to do, in terms of the game logic. It’s impossible to tell from the isolated nature of the question whether or not any form of wait-based loop is even the best solution for what you want to do.

1 Like