Help with DataStore2 and getting Server-Side values in localscripts

I’ve recently switched over to DataStore2, and have decided to make the player’s Stats Server-Side, so that exploiters can no longer access them. The problem is that since the stats are no longer located in a place where local scripts can access them, the local scripts in my game that require values from those Stats can’t function anymore. How would I get the value of said Stats, without having to place a copy of the Stats in Replicated Storage, or excessive Invokes to check the Stat’s value?

3 Likes

You can still create values in the Player instance upon PlayerAdded so that those values can be updated by DataStore2 while still being accessed by LocalScripts.

2 Likes

Regardless of what method you choose, you’re going to find yourself ending up needing to create a cache of player data in some form. The two you listed are the well known ones.

Holding a copy of stats in ReplicatedStorage means that clients can get live updates of their stats, though they can also get others’ data. A RemoteFunction bridge means that a client can only query their own data provided you set it up properly. In this respect though, many invokes where not necessary sounds pretty awful for network traffic.

Something you could also do is have the client create a version of their stats: at least the ones that they should be seeing with instantaneous feedback. Drawback is how to get those values updated. I was thinking that the server can listen for changes to those values and fire an event to the client to transfer data. That introduces receive time as a bottleneck though and potentially a race condition.

1 Like

you can simply just a hold a copy of all the values on the client and update them with remote events from the server whenever there value changes, and you would fire that remote event on the specific player whose data changed.

2 Likes

A RemoteEvent can accept a table with indexes. The only thing it can’t pass is functions and non-instance userdatas (created using newproxy(true), or the math, table, etc globals).

I’d recommend sending a table containing the data from the data store and updating keys when necessary. This table can be stored in an empty ModuleScript which can be accessed on the client. These values can’t be changed by the client server-sided.

1 Like

It really depends on what type of stats. Could you please tell us the stats you are trying to secure.