When I load into the game, I have 0 money on the leaderstats counter, even tho in the profile.data.gold i have 450, then when i claim lets say 50, it goes from 0 to 500, so I'm guessing that there's a load timing issue but idk how to fix it, here's the code, any help would be greatly appreciated:
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local ServerScriptService = game:GetService('ServerScriptService')
local ProfileStore = require(ServerScriptService.Libraries.ProfileStore)
-- The PROFILE_TEMPLATE table is what new profile "Profile.Data" will default to:
local PROFILE_TEMPLATE = {
gold = 0
}
-- local Players = game:GetService("Players") -- Duplicate declaration, already defined above
local function GetStoreName()
if RunService:IsStudio() then
return "Test"
else
return "Live"
end
end
local DataManager = require(ServerScriptService.DataManager)
local PlayerStore = ProfileStore.New(GetStoreName(), PROFILE_TEMPLATE)
--local Profiles: {[player]: typeof(PlayerStore:StartSessionAsync())} = {}
-- code we want to run when everything is loaded
local function Initialize(player: Player, profile: typeof(PlayerStore:StartSessionAsync()))
print("Gold loaded from profile:", profile.Data.gold)
local leaderstats = Instance.new('Folder')
leaderstats.Name = 'leaderstats'
leaderstats.Parent = player
local gold = Instance.new('IntValue')
gold.Name = 'Gold'
gold.Value = profile.Data.gold
gold.Parent = leaderstats
end
local function PlayerAdded(player)
-- Start a profile session for this player's data:
local profile = PlayerStore:StartSessionAsync('Player_'..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()
DataManager.Profiles[player] = nil
player:Kick(`Profile session end - Please rejoin`)
end)
if player.Parent == Players then
DataManager.Profiles[player] = profile
print(`Profile loaded for {player.DisplayName}!`)
-- The line "profile.Data.gold += 0" was here and has been removed.
-- You should set "Cash" in PROFILE_TEMPLATE and use "Profile:Reconcile()",
-- otherwise you'll have to check whether "Data.Cash" is not nil
Initialize(player,profile)
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 = DataManager.Profiles[player]
if profile ~= nil then
profile:EndSession()
end
end)
** You are now Level 2! **