A replicated state manager for Roblox.
Freon is still in beta, so expect some issues.
- Freon is Efficient. It will only Replicate state when updated to avoid unnessesary client updates.
- Automatically pulls server state to client when a player joins.
- Data is Secure. Client is never trusted with replicated state updates.
- Freon's replication is optional. You can use Freon to create state that exists souly on the client or server.
- One module for both the server and the client.
- Freon is only able to accept primitive types within its state.
- For security, Freon's Replication is strictly downstream (Server to Client)
Note: The [Owner Only] flag denotes that the method can only be used on Objects that were created Locally (Server Calling Server Created State or Client Calling Client Created State)
Freon.new(Key: string, InitState: {any}, Recipients: {Player?} | Player?, IsPermanent: boolean?)
Creates a new
Freon Instance
given anKey
and initalState
.Note: If an instance is created on the server, it will replicate to the given Recipients. Leave this empty or nil if you want Freon to replicate this state to all clients.
Freon.new("Globals", { MyAwesomeKey = "MyAwesomeValue", MyFavoriteNumber = 10 }, Freon.AllPlayers, true)
[Yeilding]
State:await(Key: string): Freon
Waits for a
State
to replicate given a key. Returns aFreon Instance
.local State = State:await("Globals")
State:get()
Returns the Current
State
of ourFreon Instance
.local Data = State:get() print(Data.MyFavoriteNumber)
[Owner Only]
State:set({any})
Completely Overwrites the State of a Freon Instance.
State:set({ MyFavoriteNumber = 10 }) print(State:Get().MyFavoriteNumber)
[Owner Only]
State:update(Key: string, Value: any)
Update a single key within the state.
State:update(MyFavoriteNumber, 12) print(State:Get().MyFavoriteNumber)
State:onUpdate(Callback -> (CurrentState: {any}))
Fires when state is modified. Passes current state to Callback.
State:onUpdate(function(CurrentState) print(CurrentState.MyFavoriteNumber) )
Never attempt to modify a
Freon Instance
directly. Freon's internal state is read only.-- Bad State.State = {"Test"} -- Good State:set({"Test"})
Never expect Client state to be accessable by the server. Freon is intentionally unable to replicate from Client to Server for security. Any attempt to do so will throw an error.
Here is an example of a simple countdown.
--> Server -->-----------------------------------
local Freon = require(game.ReplicatedStorage.Modules.Freon)
--> New State Object (Key: string, InitialValue: {any}, Recipients: {Player} | Player | boolean?, IsPermanent: boolean?)
local State = Freon.new("Globals", {
GameTimeLeft = 10,
PlayersLeft = 0
}, Freon.AllPlayers, true)
--> Countdown
for i = 9, 1, -1 do
task.wait(1)
--> Update State
State:update("GameTimeLeft", i)
end
--> Client -->-----------------------------------
local Freon = require(game.ReplicatedStorage.Modules.Freon)
--> Wait for state (Key: string)
local State = Freon:await("Globals")
--> Print Current State
print(State:get().State.GameTimeLeft)
--> On Update, Print the remaining game time.
State:onUpdate(function(State)
print(State.GameTimeLeft)
end)