Hi there, I’m currently making a stats with datastore. I’m trying to replicate the player data from the server to the client.
Main data module:
local PlrData = {}
PlrData.__index = PlrData
local transferData = require(script.Parent.transferData)
function PlrData:newData() -- Create new data if the player is new
local newData = {
PlrStats = {
Strength = 1,
Agility = 1,
Magic = 1,
Vitality = 1,
Defense = 1,
},
}
return newData
end
local SavingFor = {}
function PlrData.init(plr)
local plrTable = {}
local self = setmetatable({}, PlrData)
self.dataStore = game:GetService("DataStoreService"):GetDataStore("TEsT")
self.key = plr.UserId .. "'s Data"
self.table = plrTable
return self
end
function PlrData:LoadData(plr) -- load the data of the according player
local Data
local suc, err = pcall(function()
Data = self.dataStore:GetAsync(self.key)
end)
if not suc then
return warn(err)
end
self.table[plr.UserId] = Data or self:newData()
transferData:UpdateData(plr, self.table[plr.UserId])
end
There are more code to the main module but they are irrelevant to this post.
transfer data module:
local transferData = {}
local plrData = {}
local sendData = script.sendData
local RunService = game:GetService("RunService")
local function printTable(tab, string)
for i,v in pairs(tab) do
if type(v) == "table" then
print(string,i," : ",v,":::")
printTable(v, string.." ")
else
print(string,i," : ",v)
end
end
end
function transferData:UpdateData(plr, tbl)
if RunService:IsServer() then
plrData[plr.UserId] = tbl
sendData:FireClient(plr, tbl)
else
plrData[plr.UserId] = tbl
end
end
function transferData:GetData(plr)
printTable(plrData[plr.UserId], "")
return plrData[plr.UserId]
end
if RunService:IsClient() then
sendData.OnClientEvent:Connect(function(tbl)
local plr = game.Players.LocalPlayer
transferData:UpdateData(plr, tbl)
end)
end
return transferData
It gets replicated perfectly when its server - server, since I set the self.table
to the plrData[plr.UserId]
in the transfer data module. I thought it would work for the client as well, but turned out it doesn’t. Does this means I would have to send a remote manually everytime the player’s data gets updated ? It feels very inefficient to me. I have tried the __index
and __newindex
metamethods to send the wanted data / value to the client but it doesn’t work and I have asked for help in other posts.
Any alternatives ?