How to replicate Player's data to client?

Hi, i don’t know how can i make items, tools ect. visible on client, player’s data is stored inside server storage, any solutions for this? or should i place data in other place

workspace and ReplicatedStorage replicate

1 Like

When you say the data is in ServerStorage, what do you mean? Could you elaborate a bit more on that? Can you send a screenshot/code snippet explaining what you mean?

1 Like

this means folder with player data is created in server storage, it contains all of data like coins, items ect. and then i wan’t to replicate data from there to client

I’m still struggling to understand. Can you explain what value types you use, e.g. IntValues? An example screenshot would be very helpful.

1 Like

As mentioned here, use ReplicatedStorage, as it can be accessed by both the client and server.

1 Like

Or if it’s player specific data, you can parent it to the player instance.

1 Like

here is screenshot
Zrzut ekranu 2024-01-08 201251

Easy, instead of making a folder for each player, parent it to the player instance.

1 Like

This replicates to both client and server.

i’m doing it on server because sometimes player destroys before data is saved

Then use ReplicatedStorage if you prefer.

1 Like

It looks like you can take the data from here and put it into a table into a server-side Script. You could create a function for this.
Code for inside the function:

--where 'Player' is the player object
local folder = game.ServerStorage.PlayerData:WaitForChild(player.Name)
local data = {
    ["Coins"] = folder.Coins.Value,
    ["Gems"] = folder.Gems.Value
}

game.ReplicatedStorage.RemoteEvent:FireClient(player, data) --pass it down a RemoteEvent to the client

It would be more efficient to keep this folder within the player, that is if the folder is even necessary. You could pass this straight down a RemoteEvent when the data is loaded, completely skipping the folder process.

1 Like

Nothing in the ServerStorage is replicated to the client. This is the purpose of it, to provide security for things you don’t want them to see.

I recommend either placing it under ReplicatedStorage, or under the player, but when they leave the game, save it using DataStoreService.

1 Like

When player is leaving, keep the player UserId as a variable and store all data values into a table/dictionary before saving them, so everything is safe and you are able to save even if the players object is destroyed.

1 Like

is having a table of variables and then replicate it to client good?

It is certainly more efficient than having a lot of Instances physically stored server-side for each player, which would take up a lot of memory.

1 Like

Yep, you could use a RemoteEvent and send the table to the client.

local DataRemote = ReplicatedStorage:WaitForChild("DataRemote")

local PlayerData = ServerStorage:WaitForChild("PlayerData")
local MyData = PlayerData and PlayerData:WaitForChild(Player.Name)
local DataTable = {}

for _, Value in pairs(MyData:GetChildrens()) do
    DataTable[Value.Name] = Value.Value
end

DataRemote:FireClient(Player, DataTable)

but then, replicating it would be performance heavy, imagine fire remote event to client every variable changed

That would be more efficient, considering when the player’s data is loaded, if you use a dictionary to store the data in the first place, you could just pass the dictionary to the client which would allow both sides access to it.

Bear in mind players join at different times, so it would not necessarily happen all at once, for each player.

1 Like