#Problem loading from ProfileStore

1 messages · Page 1 of 1 (latest)

plush lion
#

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, and I'll attach a video that ilustrates whats happening any help would be greatly appreciated:

Datainit

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}!`)
            profile.Data.gold += 0
            -- 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)
#

Sell:

local ProximityPromptService = game:GetService('ProximityPromptService')
local ServerScriptService = game:GetService('ServerScriptService')
local sellMat = game.Workspace.SellingTable.SellingMat
local DataManager = require(ServerScriptService.DataManager)

--making the bag apear
sellMat.Touched:Connect(function(hit)
    local model = hit:FindFirstAncestorOfClass('Model')
    if model and model:GetAttribute('Value') then
        local value = model:GetAttribute('Value')
        local modelCFrame = model:GetPrimaryPartCFrame()
        print(value)

        model:Destroy()

        local moneyBag = game.ServerStorage.MoneyBag:Clone()
        moneyBag.Parent = workspace
        moneyBag.CFrame = modelCFrame + Vector3.new(0,1,0)
        moneyBag.Orientation += Vector3.new(0,0,-90)
        moneyBag:SetAttribute('Value', value)
        moneyBag.ProximityPrompt.ActionText = 'Claim '..value..'$'
    end
end)

local function onPromtptTriggered(promptObject, player: Player)
    local value = promptObject.Parent:GetAttribute("Value")

    -- Better way to print player info
    print(player.Name.." received "..value.." gold")

    DataManager.AddGold(player, value)
    promptObject.Parent:Destroy()
end

--Connect promt to handling function
ProximityPromptService.PromptTriggered:Connect(onPromtptTriggered)

DataManager:

local DataManager = {}

DataManager.Profiles = {}

function DataManager.AddGold(player: Player, amount: number)
    local profile = DataManager.Profiles[player]  -- Fix the typo here
    if not profile then return end

    -- Ensure Gold exists before adding
    profile.Data.gold = profile.Data.gold or 0
    profile.Data.gold += amount

    -- Update leaderstats safely
    local leaderstats = player:FindFirstChild('leaderstats')
    if leaderstats then
        local goldStat = leaderstats:FindFirstChild('Gold')
        if goldStat then
            goldStat.Value = profile.Data.gold
        end
    end
end
return DataManager
dapper schooner
#

jist use regular datastore bruh

#

its simpler

plush lion
#

nvm i just realised what happend when cping the code

plush lion
dapper schooner
#

and u can whip 1 out in like 15 lines

#

it works as well as the profile

#

I guess if you want like a

plush lion
#

i watched a vid and said to use this, ill look into this then

dapper schooner
#

more complicated game u cpuld go with profile but Id go to data

#

yeah profile store is good when u have many like things to save

plush lion
#

Ohhh so like inventory system and things ;like that

#

okay, thanks a lor, much apreeciated <3

#

appreciated