UnifiedData
Very very simple module for managing trivial data that is shared from the server to the client. The intended use-case is for when the server has a large but mostly non-volatile amount of data that needs to be synced to all clients and inconvenient to facilitate outside of code. The aim is to be as simple to use as possible and effectively be the same as just modifying and reading a normal table.
Relevant documentation can be found here,
the source code is available on my repo,
and the model can be grabbed directly from the marketplace.
The following example is grabbed directly from the documentation.
Example
In this very simple example, we’ll create the best game ever conceived; it rewards you for simply existing!
Or in boring words, the player’s score goes up as they stay in the game.
Assumptions
The client and server should have access to the same UnifiedData
ModuleScript. In this example, it’ll just be a direct child of ReplicatedStorage
.
The code under Server is located in ServerScriptService
while the code under Client is located in StarterPlayerScripts
.
Server
--!strict
local Players = game.Players
-- We specifically grab the Server code for UnifiedData
local UnifiedData = require(game.ReplicatedStorage.UnifiedData.Server)
-- The second argument of CreateTable is optional;
-- We could set it if say we wanted the creator to start with 9999 score
-- This returns a ServerProxy; however by overriding the return type,
-- it prevents mistakes when using strict type-checking
local ScoreTable = UnifiedData.CreateTable("Score") :: {[string]: number}
local function onPlayerJoined(player: Player)
if ScoreTable[player.Name] == nil then ScoreTable[player.Name] = 0 end
task.spawn(function()
while player.Parent ~= nil do
wait(1)
ScoreTable[player.Name] = ScoreTable[player.Name] + 1
end
end)
end
for _, player in pairs(Players:GetPlayers()) do
onPlayerJoined(player)
end
Players.PlayerAdded:Connect(onPlayerJoined)
Client
-- Here we grab the Client code for UnifiedData
local UnifiedData = require(game.ReplicatedStorage.UnifiedData.Client)
local Player = game.Players.LocalPlayer
-- We could potentially use UnifiedData.GetProxy instead, however
-- there's a chance that the data hasn't replicated yet, necessitating
-- that we use UnifiedData.WaitForProxy instead.
local ScoreTable = UnifiedData.WaitForProxy("Score")
ScoreTable.Changed:Connect(function(name)
if name == Player.Name then
print(`Score is now {ScoreTable[name]}`)
end
end)
wait(10)
print(`My score is currently {ScoreTable[Player.Name]}`)
With all the code in place, when testing in studio you should see print statements in the output reporting the score for you, while effectively all you’ve done is make a table and modify it on the server and read from it on the client.
Please feel free to give some feedback regarding the module. I made this with the intention of using this for myself but thought it could be potentially useful for a lot of people as a tool with a very simple interface.