Skip to content

Basic Example

Server

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
--!strict
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local StateService = require(ReplicatedStorage.Modules.StateService)

StateService:AddState(
    workspace,
    "GameStats",
    {
        PlayersOnline = 0
    },
    nil,
    {
        OnEdited = function(
            OldValue,
            NewValue
        ): ()
            print("Game stats updated - PlayersOnline:", NewValue.PlayersOnline)
        end
    }, {
        ClientReadable = true
    }
)

Players.PlayerAdded:Connect(
    function(Player: Player): ()
        -- Create PRIVATE player profile
        StateService:AddState(
            Player,
            "PlayerData",
            {
                Example = "Hello World!"
            },
            nil,
            {
                OnRemoved = function(
                    Value,
                    ExpiredByLifespan
                ): ()
                    print(`{Player.Name}'s data removed: {ExpiredByLifespan and "expired" or "manual"}`)

                    print(`Saving Example: {Value}`)

                    -- Saving stuff here
                end,
                OnEdited = function(
                    OldValue,
                    NewValue
                ): ()
                    if OldValue.Example ~= NewValue.Example then
                        print(`{Player.Name}'s Example changed from {OldValue.Example} to {NewValue.Example}`)
                    end
                end,
                OnCalled = function(
                    Caller,
                    ...
                ): ()
                    print(`Player data accessed via: {Caller}, for player: {Player.Name}`)
                end
            },
            {
                ClientReadable = true,
                AuthorizedPlayers = {
                    Player
                }
            }
        )

        -- Create PUBLIC player profile
        StateService:AddState(
            Player,
            "PublicProfile",
            {
                DisplayName = Player.DisplayName
            },
            nil,
            nil,
            {
                ClientReadable = true
            }
        )

        local GameStats = StateService:GetState(
            workspace,
            "GameStats"
        )

        if GameStats then
            (StateService :: any):EditState(
                workspace,
                "GameStats",
                {
                    PlayersOnline = #Players:GetPlayers()
                }
            )
        end
    end
)

Players.PlayerRemoving:Connect(
    function(Player: Player): ()
        local GameStats = StateService:GetState(
            workspace,
            "GameStats"
        )

        if GameStats then
            (StateService :: any):EditState(
                workspace,
                "GameStats",
                {
                    PlayersOnline = math.max(
                        0,
                        #Players:GetPlayers() - 1
                    )
                }
            )
        end
    end
)

Client

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
--!strict
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Player = Players.LocalPlayer

local Modules_RS = ReplicatedStorage:WaitForChild("Modules")

local StateService = require(
    Modules_RS:WaitForChild(
        "StateService",
        60
    ) -- WaitForChild is crucial for the client.
)

print(StateService:GetState(Player, "PlayerData"))

API Reference: API