I need help if this will cause lag for 100 players

I have a script on the client that loops all the players in the workspace and requires their Data/Stats Module and sets their character stats accordingly. This loops every 0.1 second. Will this cause too much threads and lag if there where a 100 characters/players in the workspace?

When a module gets deleted does it automatically disconnect all threads?

1 Like

Is it really necessary to do this every 0.1 seconds? I would have a guess that there is probably a better way to do what you are trying to achieve (but maybe not, just expand on why you need to it so frequently please).

When a player updates there character data I want all the clients to see it.

1 Or 2 seconds would be plenty fine,
Then your dividing the lag by x10 or x20, it doesn’t need to be instant as ping is a thing anyway which for most is over 0.1 second

The best solution would be to run the event WHEN a user changes their data to make the game more optimized.

1 Like

The approach you are using wastes a lot of processing resources because the program will try and update the player’s data even when there are no changes to the data (effectively updating nothing, and using up valuable procesing time). Having said this, computers are very powerful today and so I would estimate that there would probably be minimal drop in performance from running something like this. However, that doesn’t mean it’s not bad programming practice to do it this way (please don’t do it like this).

A better approach is to update the data for all clients only when the data changes. This way you do not need to be running a loop every 0.1 seconds.

1 Like

As stated above, it’s more performant if you update only when the data changes.

To achieve that, I would suggest linking a RemoteEvent from the server to the client that is fired when data changes, and only provides the changed data to each client. The client should then have code that knows how to apply the data accordingly.

That would waste not only processing power but also DataStore rate limits.

Here is the most efficient way:

  • When a player joins the game, get their data from the datastore and cache it in a value like NumberValue, StringValue or BoolValue.
  • This code will bind a function to the .Changed event of the value (so you don’t have to loop):
value.Changed:Connect(function(newValue)
	-- Code here.
end)
  • When the player leaves, save the cache to the datastore and remove the cache values.

We can’t assume that they’re getting/setting anything to a DataStore based off of the information that we’ve been given. Furthermore, I would also counter your suggestion of writing player data to value objects such as NumberValues, StringValues, etc. as that will also waste more resources than necessary.

While the cache is a good idea, the cached data should be saved in a simple table within the script that is managing player data. That’s the most efficient way to store and manage it.

For reasons I can not do it when it changes because the data is in a module script and I need to update it when the module gets sent to the client .

I made sure to do it every 2 seconds

and

set the required module variable to nil at the end of each loop thread