ModuleScript or JSON.Encode/Decode?

Hello everyone. Since there is no TableValue like IntValue or NumberValue instance, I have been saving complex tables as my own formatted StringValues.
But it’s getting too complicated to handle, and wasting a lot of my time as a dev. I know JSON.encode and decode will be a lot easier, but is it as efficient, especially as it is being sent somewhere i think. (because it’s httpservice i dont know)
Or I could use ModuleScripts and use remoteEvents each time a value changes and update the client. But is sending like 10 remoteEvents/sec efficient because of the amount of number changes?

Let me know what you guys think :slight_smile:

It is not efficient to send an entire table over the network each time a minor component changes. Instead, send the key and value for the client to update. Send the complete table (without encoding it) initially so the client has a copy to work with

2 Likes

The methods JSONEncdoe and JSONDecode do not send the contents to be encoded or decoded anywhere. These functions are only packaged with HttpService because the use of these formats is often associated with HTTP requests.

The correct approach to sending information across the client/server boundary can vary by use case. It is possible to send tables across from server to client or vice versa with some exceptions. If the amount of data is large and the rate of sending updates is frequent, you may want to serialize this data in some form, compressing it before sending on one side and decompressing it on the other side.

1 Like

How would I compress the list?

It depends on what sort of information your table contains. Could you elaborate on what system is being implemented and what information you expect needs to be replicated?

Like an inventory list. Something like this {[‘Dog’] = {[‘PWR’] = 4, [‘ATK’] = 10, [‘Cat’] = {[‘SPD’=4} etc

Again, you’re not likely to send this data more than once. If the inventory changes, only inform the client of what changed—do not send a new copy of the entire inventory. For serializing tables in general, you can look into Squash’s table serialization options

3 Likes

For an inventory system, the server must first send the client the entire contents of their inventory. After this first “sync”, the client only needs to know about changes to the inventory - the client does not need to recieve a copy of their entire inventory every time something changes. However, in order for the client to stay in sync with the server (which holds the trusted copy of the inventory), the server must inform the client every time the inventory changes.

There’s many ways to approach this. A simple approach might incorporate two RemoteEvents:

  • SyncInventory
  • UpdateInventory

When a Player joins the game, the server will load their inventory and send its entire contents using the SyncInventory RemoteEvent. Later, after the server applies any changes to the that player’s inventory, it sends an update over the UpdateInventory RemoteEvent.

The payload sent over the SyncInventory RemoteEvent could be the table the server keeps in its entirety, whereas the payload sent over the UpdateInventory RemoteEvent could be two values: the key in the inventory to update, and the new value at that key.

Since the contents of players’ inventories are unlikely to be changing at a high rate, there is probably not much need for serialization.

1 Like

@Ziffixture @East98 Thank you for your help! I will implement this soon and I’ll let you know if I encounter any issues.

For me i would create an inventory folder inside of player then i would add items as StringValue refers to items name when player gets items. These items has stringvalue or intvalue childs inside for holding attributes or you can also hold attributes using attribute system in properties section of each item which is my favourite. Then load/save this data on player joins/quits using own datastore system of roblox. You can save tables on this datastore as this hierarchy: playerdata = {}, playerdata[1] = inventory, inventory[1] = item1, inventory[2] = item2… loop the inventory on load/save to get/set items

1 Like

Wouldn’t a module script do essentially the same thing but its a bit easier?

Also, what are some memory leaks I should look out for when storing data in module script?

out of curiosity, what would you use this for? i’m interested to learn more about it (-:

I store my inventory and quest-targets in stringvalues, but its super annoying and sometimes impossible to work with because you have to use insanely complex string.match sequences which is extremely inefficient

1 Like

oh i see! thanks for the explanation. i might get into it down the line but its a bit out of my depth atm but best of luck to u !! (-:

1 Like

It took me about 5 years to get to where I am now, the most important thing is you keep scripting

1 Like

ofc man. its all about step by step, start to finish. appreciate the advice king

1 Like

So far this is my implementation,

there is a datascript module with plrData and at playerAdded use async remote to transfer plr data to a local script, then use bindable events to transfer the data. Then when updatedata remote is called just update a certain part of the table.

However, I am unsure if it will scale well to large servers, as my game is sort of a clicker game, so 20-60 remote events (data updates) / second wouldn’t work too well.

What should I use instead of .Changed on my currency labels, since you can only have 1 .OnInvoke?