A more efficient way to monitor a value that constantly updates?

Hey everyone, I am more or less just looking for feedback about whether or not this approach is okay for me to take.

In my particle simulation game, I keep a constant tally on how many particles have ever spawned & how long the server has existed for. I understand that these two hanging threads are relatively acceptable to have in my game; however, I also monitor these values, because once they reach a certain amount I upgrade the “server level” (more or less just a system I’ve been writing in to encourage longer playtime).

At the moment, these two values are being monitored like so:

spawn(function()
	while true do
		sWait(1)
		Uptime = Uptime + 1
		--print('Uptime: '..Uptime)
		
		if TotalParticlesUsed >= 1000 and Uptime >= 180 then
			--print('server knows! sending to client')
			WorldLevel = 2

			workspace.Atmosphere.WorldLevel.Value = 2
			Remotes.WorldLevelReplication:FireAllClients(2, 400)
		end
	end
end)

As you can see, because of its nature I feel required to check on these conditions every time either the uptime changes or the total particles spawned changes. Ideally though, I would like to largely ignore these two variables until they’ve reach their top values. I worry that if I leave it like this, although it would work, it would have a pretty big impact on performance over time.

These values change very often (more than once per second in the case of ParticlesUsed), so TL;DR I am searching for some way to outright know when the requirements have been reached- but without running the check so many times.

It may just be wishful thinking, I’m also very tired so it might also be that I just cannot think of it right now.

Clarification can be provided upon request, and all help is appreciated. :slight_smile:

Can’t you just listen to GetPropertyChanged signal? + I think there’s property in workspace that is tracking the Server uptime.

https://developer.roblox.com/en-us/api-reference/property/Workspace/DistributedGameTime

@SilentsReplacement Sorry I thought it is Value not variable.

If its a variable, you can have a setter function which increments it and whenever it is called, fire an event.

local updated = Instance.new("BindableEvent")

local function IncrementUpTime(increment)
    if UpTime == UpTime + increment then return end
    UpTime += increment
    updated:Fire()
end

@caviarbro No, GetPropertyChangedSignal is for events, not for variables.

2 Likes

Appreciated, I’ll work on this tomorrow it’s very late here lol.

I did not know about DistributedGameTime’s existance either, so I will probably switch to that for uptime instead of my own variable. I will however set up a BindableFunction for my secondary variable with the total particles spawned.

Thank you both.

2 Likes