Help with Replica module of loleris

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)

Obs: im using profileservice too

2 Likes

Try using this tutorial:
(I used this tutorial on my game and it worked)

1 Like

thank you, but im using Replica to handle the data replication.

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)
1 Like

im using the multiple-clients test in Studio, but it only keeps the replica in the cache for player 1 for player 2 it just gives nil

could you show the script??
sounds weeeeird..

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

i think the problem is the way im using the replica module, i will debug the code

I found the error its because every time a player joins, its duplicating the token.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.