Basic Usage
Server
local DATA_TEMPLATE = {
fruit = {
apple = false,
banana = false,
berries = {
strawberries = 0,
raspberries = 10,
},
},
money = 0,
-- example of something we dont want to replicate!
serverFlags = {
isCool = false,
},
}
local Players = game:GetService("Players")
local Replicate = require(path.to.Replicate).Server
local replicates = { }
task.spawn(function(): nil
-- ever 5 minutes award active players some money!
while task.wait(300) do
for _, replicate in replicates do
replicate:Increment({ "money" }, 50)
end
end
return nil
end)
local function playerAdded(player: Player): nil
local newReplicate = Replicate.new(player, DATA_TEMPLATE, {
SourceMode = "copy"
-- DATA_TEMPLATE.serverFlags wont be replicated no matter what changes we make to it!
Filter = { "serverFlags" },
FilterMode = "exclude",
})
replicates[player] = newReplicate
newReplicate:Increment({ "money" }, 100) -- 100 money for joining!
-- now that we've set all the data, time to replicate to the player!
newReplicate:StartReplicating()
if player.Name == "Roblox" then
-- wow this person is really cool!
-- dont worry, this wont be replicated to them!
newReplicate:SetValue({ "serverFlags", "isCool" }, true)
end
return nil
end
local function playerRemoving(player: Player): nil
if replicates[player] then
replicates[player]:Destroy()
replicates[player] = nil
end
return nil
end
for _, player: Player in Players:GetPlayers() do
task.spawn(playerAdded, player)
end
Players.PlayerRemoving:Connect(playerRemoving)
Players.PlayerAdded:Connect(playerAdded)
Client
local Replicate = require(path.to.Replicate).Client
local clientReplicate = Replicate:GetReplicate()
-- for reading data, its okay to reference it directly instead of using
-- :GetValue()
local currentMoney = clientReplicate.Data.money
clientReplicate:GetValueChangedSignal({ "money" }):Connect(function(newValue: number, oldValue: number): nil
currentMoney = newValue
print("Player money changed from:", oldValue, "to:", newValue)
return nil
end)