Im not sure if I am not using this properly but please somebody help me!
local ProfileStore = require(game.ServerScriptService.ProfileStore)
local PROFILE_TEMPLATE = {
Money = 0,
Items = {},
}
local players = game:GetService("Players")
local playerstore = ProfileStore.New("playerstore", PROFILE_TEMPLATE)
local Profiles: {[Player]: typeof(playerstore:StartSessionAsync())} = {}
local function plrAdded(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local money = Instance.new("IntValue")
money.Name = "Money"
money.Parent = leaderstats
local profile = playerstore:StartSessionAsync(`{plr.UserId}`, {
Cancel = function()
return plr.Parent ~= players
end,
})
if profile ~= nil then
profile:AddUserId(plr.UserId)
profile:Reconcile()
if plr.Parent == players then
Profiles[plr] = profile
print(`Profile loaded for {plr.DisplayName}!`)
money.Value = profile.Data.Money
money:GetPropertyChangedSignal("Value"):Connect(function()
profile.Data.Money = money.Value
end)
else
profile:EndSession()
end
end
end
for _, plr in players:GetPlayers() do
task.spawn(plrAdded, plr)
end
players.PlayerAdded:Connect(plrAdded)
players.PlayerRemoving:Connect(function(plr)
local profile = Profiles[plr]
if profile ~= nil then
profile:EndSession()
end
end)
game:BindToClose(function()
for _, plr in players:GetPlayers() do
local profile = Profiles[plr]
if profile ~= nil then
profile:EndSession()
end
end
end)