I need some help. I’m using @loleris’s replica module, but I have no clue how to change it on the server outside of PlayerAdded. I’m not really sure how to explain it, so I’ll drop the code so you can understand my problem better.
Players.PlayerAdded:Connect(function(player)
local data = Saving.New(player)
if not data then player:Kick('Your data failed to load!') end
local leaderstats = Instance.new('Folder')
leaderstats.Name = 'leaderstats'
leaderstats.Parent = player
local Coins = Instance.new('StringValue')
Coins.Name = 'Coins'
Coins.Value = tostring(data.Currencies.Coins)
Coins.Parent = leaderstats
DataTransmitter:FireClient(player, data)
local DataReplica = Replica.New({
Token = Replica.Token('GlobalData'),
Tags = {UserId = player.UserId},
Data = Saving.GetData(player)
})
DataReplica:Subscribe(player)
--DataReplica:Replicate()
end)
EquipOre.OnServerEvent:Connect(function(player, ore, slot)
local data = Saving.GetData(player)
if not data then return end
-- how can i request the replica here, cuz i need to set the new equipped ore
data.EquippedOres[ore] = slot
end)
have you tried storing the replica table outside of the PlayerAdded? you can try something like this:
local cache = {}
local function OnPlayerAdded(player)
cache[player] = replica.New()
end
local function OnPlayerRemoved(player)
cache[player] = nil
end
local function GetReplicaWithPlayer(player)
return cache[player]
end
serverevent.OnServerEvent(function(player, ...)
local replica = GetReplicaWithPlayer(player)
if not replica then
return
end
replica:Set()
end)
local cache = {}
Players.PlayerAdded:Connect(function(player)
local data = Saving.New(player)
if not data then player:Kick('Your data failed to load!') end
local leaderstats = Instance.new('Folder')
leaderstats.Name = 'leaderstats'
leaderstats.Parent = player
local Coins = Instance.new('StringValue')
Coins.Name = 'Coins'
Coins.Value = tostring(data.Currencies.Coins)
Coins.Parent = leaderstats
local PlayerToken = Replica.Token('PlayerData')
local replica = Replica.New({
Token = PlayerToken,
Tags = {UserId = player.UserId},
Data = data
})
cache[player] = replica
--replica:Replicate()
end)
Replica.NewReadyPlayer:Connect(function(player)
cache[player]:Subscribe(player)
end)
Players.PlayerRemoving:Connect(function(Player)
Saving.Release(Player)
cache[Player] = nil
end)
--Replica get data function on client
local cache = {}
local connection;
connection = Replica.OnNew('PlayerData', function(replica)
if replica.Tags.UserId == LocalPlayer.UserId then
cache[LocalPlayer] = replica
end
connection:Disconnect()
end)
function Data.GetData(Player)
while not cache[LocalPlayer] do task.wait() end
return cache[LocalPlayer]
end
Replica.RequestData()
--it gives a error, ReplicatedStorage.ReplicaServer:261: [ReplicaServer]: Token "PlayerData" duplicate
i dont see any reason for it to not work
not sure if player2’s replica is just destroying itself for no reason, the script is slow, player2’s data is not loading, or playerremoving is firing for 0 reason
which none of these sound plausible
i guess you should just try adding prints where replica, or cache[key], could become nil