What would be an efficient way to transfer data from server to client?

I’m debating on how should I make the data sharing between server and client work, i thought of 2 solutions and I’m not sure which will be the most efficient one performance-wise. so i have all players data on the server, for example:

allPlayersData = {
  [player1.UserId] = {level = 1, gold = 5, items = ["wooden sword"]},
  [player2.UserId] = {level = 5, gold = 50, items = ["steel sword"]}
}

now, player1 can see player2 data only when they are clicking their username on leaderboard, but they can see their level all of the time because it’s shown on the leaderboard.

i thought of 2 options of how to make it work.

  1. make player request allPlayerData for the first time and then listen to change events that is fired from the server to all clients whenever a piece of data have been changed, which for me sounds like it could be a performance issue because there will be a lot of event fires from the server to clients.

  2. have a level intValue under every player, so that the player that wants to know the level of the other player can just read it from there, and only when player presses specific player on the leaderboard it’ll send request to the server to get that players data, the answer could be slow, but i feel like it’ll be a little more efficient then option 1.

i’m not sure which option i should go with, or is there a better option?

also another question, how do i access data (that is stored in a variable) that for example LocalScript1 got from the server from LocalScript2? it’s so that i wouldn’t have to request data from server on every localscript.

To me personally, best way to store data would be in a Value objects. Like, StringValue/NumberValue etc. Because when they get changed on server Client also sees the changes, hence you don’t need to “share” the data constantly but just check the value. And then instead of rewriting the variable you rewrite the Value object itself, also save the object’s value instead of variable. All basic DataStore scripts or most of data storing based of ValueBase.

You can use remote event and a Raycast maked by mouse.UnitRay properties.

All moment they click and the Raycast is pointing to a player, they will request the player data to server and return to client.