Skip to content

Share state with the server

The server owns what matters: the round, the scores, who signed up, each player’s coins. Lucy.Server keeps that in the same kind of store your App has, sends players what they may see, and takes their requests. Your UI binds to it like any other app state, with no attributes or remotes to wire up.

On the server

Make one server, early, in a server script:

local Lucy = require(ReplicatedStorage.Packages.Lucy)

local server = Lucy.Server.new({
    state = { shared = { show = { phase = "Signup", round = 1 } } },
})

server.state has the store’s methods: get, set, update, merge, remove, subscribe and batch (see The store). Where you put a value decides who sees it:

PathWho sees itIn their App at
shared.*every playerserver.*
players.u<UserId>.*that player onlyserver.me.*
anything elsenobody: it stays on the server
server.state:set("shared.show.phase", "Live")      -- everyone
server:player(player):set("coins", 50)             -- this player
server.state:set("rounds.seed", 1234)              -- the server only

server:player(player) is that player’s part as a store of its own. It exists from when they join until they leave. Keys in replicated state must not contain dots.

In the App

Ask for the link when you make the App:

local app = Lucy.App.new({ server = true })

local phase = app.state:ref("server.show.phase")
local coins = app.state:ref("server.me.coins")

Lucy.Text.new({
    text = function()
        return `{phase()} · {coins() or 0} coins`
    end,
    Parent = scene.gui,
})

The values arrive a moment after the App starts, so read them as possibly nil at first. Treat server.* as read only: the next update from the server replaces anything you write there. Keep your own UI state elsewhere.

Requests

Players ask the server to do things with requests. Handle each one by name, and say what it may be sent: a client can send anything, as often as it likes. check lists a check for each argument and rate how often one player may ask; the handler only runs when both pass. Make the server with strict = true and every handler must have a check.

-- Server
local Check = Lucy.Check

server:handle("buy", function(player, itemId)
    local item = ITEMS[itemId]
    if not item then
        return false
    end
    local me = server:player(player)
    if (me:get("coins") or 0) < item.price then
        return false
    end
    me:update("coins", function(coins)
        return coins - item.price
    end)
    return true
end, { check = { Check.string({ max = 32 }) }, rate = { perSecond = 2 } })
-- Client
app.server:send("vote", "House")                  -- nothing comes back
local bought = app.server:call("buy", "moonwalk") -- waits for the answer

call returns what the handler returned, and raises an error if there is no handler or it failed. Requests sent before the link has found the server wait and go when it has. Requests from a player who has left are refused.

Write related values in server.state:batch so players never see half a change. Every option and method, and how changes travel, are in The server.

Next

  • UI in the world: boards and name tags drawn from server state
  • Save player data: load a profile into the server’s store when a player joins and save it as it changes