I have used ReplicaService with ProfileService before, but would like to switch to ProfileStore with Replica. I cannot find any videos on Replica (there are resources for ProfileStore). I am not sure if my interpretation of Replica is correct. Below are basic code examples and questions:
Client-Side examples and questions
Here is my old ReplicaService example
--// Client-side: ReplicaService usage
local ReplicaController = require(game.ReplicatedStorage.ReplicaModules.ReplicaController)
ReplicaController.ReplicaOfClassCreated("Profile", function(replica)
-- Coins changed server side
replica:ListenToChange({"Coins"}, function(new_value)
-- do something with coins
end)
end)
-- run once
ReplicaController.RequestData()
And this is what I got as the Replica Equivalent.
Question 1: Did I implement the new functions correctly?
--// Client- side: Replica usage
local Replica = require(game.ReplicatedStorage.ReplicaClient)
-- this replaces the "ReplicaOfClassCreated" function?
Replica.OnNew("GlobalData", function(replica) -- I can use "Profile" instead of "GlobalData", right?
-- this replaces the "ListenToChange" function?
replica:OnSet({"Coins"}, function(new_value, old_value) -- do I have to include "old_value"?
-- do something with coins
end)
end)
-- this replaces the "RequestData" function (same thing, just different module name)
Replica.RequestData()
Server-Side examples and questions
Server-side, I am a bit more confused.
Question 2: Do I call this only once after the replica table is created the first time?
replica:Replicate()
Question 3: Is this server-side equivalent correct?
--// ReplicaService
replica:SetValue({"Coins"}, profile.Data.Coins)
-->> Replica
replica:Set({"Coins"}, replica.Data.Score.Coins
Question 4: Do I still need to destroy the replica when the player leaves like so?
Players.PlayerRemoving:Connect(function(player)
-- release profile
local profile = Profiles[player]
if profile ~= nil then
profile:Release()
end
-- destroy replica
local replica = Replicas[player]
if replica then
replica:Destroy()
end
Replicas[player] = nil
end)
Any help would be appreciated.