It has a template in the example code. Let’s say you have Bloxy Cola in your inventory, you can write a part for your player where you store a table, add the string "bloxy_cola", and give it back to the player when they join the game again.
The example is really helpful.
local ProfileStore = require(game.ServerScriptService.ProfileStore)
-- The PROFILE_TEMPLATE table is what new profile "Profile.Data" will default to:
local PROFILE_TEMPLATE = {
Cash = 0,
Items = {},
}
local Players = game:GetService("Players")
local PlayerStore = ProfileStore.New("PlayerStore", PROFILE_TEMPLATE)
local Profiles: {[player]: typeof(PlayerStore:StartSessionAsync())} = {}
local function PlayerAdded(player)
-- Start a profile session for this player's data:
local profile = PlayerStore:StartSessionAsync(`{player.UserId}`, {
Cancel = function()
return player.Parent ~= Players
end,
})
-- Handling new profile session or failure to start it:
if profile ~= nil then
profile:AddUserId(player.UserId) -- GDPR compliance
profile:Reconcile() -- Fill in missing variables from PROFILE_TEMPLATE (optional)
profile.OnSessionEnd:Connect(function()
Profiles[player] = nil
player:Kick(`Profile session end - Please rejoin`)
end)
if player.Parent == Players then
Profiles[player] = profile
print(`Profile loaded for {player.DisplayName}!`)
-- EXAMPLE: Grant the player 100 coins for joining:
profile.Data.Cash += 100
-- You should set "Cash" in PROFILE_TEMPLATE and use "Profile:Reconcile()",
-- otherwise you'll have to check whether "Data.Cash" is not nil
else
-- The player has left before the profile session started
profile:EndSession()
end
else
-- This condition should only happen when the Roblox server is shutting down
player:Kick(`Profile load fail - Please rejoin`)
end
end
-- In case Players have joined the server earlier than this script ran:
for _, player in Players:GetPlayers() do
task.spawn(PlayerAdded, player)
end
Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(function(player)
local profile = Profiles[player]
if profile ~= nil then
profile:EndSession()
end
end)