#Problem loading from ProfileStore

1 messages · Page 1 of 1 (latest)

fallow moat
#

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)

hexed ermine
fallow moat
#

0

fallow moat
#

Idk how to make it wait for it to fully load

hexed ermine
#

And what code updates it?

fallow moat
#

When I add gold, I’ll send it to you, one sec

#
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)

#

this is the script that updates it when i sell somehting

hexed ermine
#

Can i see the DataManager script?

fallow moat
#
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
hexed ermine
#

Simple typo, in your datamanager its spelled ‘Gold’ but in profile service script its ‘gold’, lua is case sensitive. You essentially load a value that will always be 0, as its never updated.

fallow moat
#

it still loads 0

hexed ermine
#

No the value not the instance

#

profile.Data.Gold not profile.Data.gold

fallow moat
#

ohhhh

#

okay oikat

#

like this ?

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

random parrotBOT
#

studio** You are now Level 2! **studio

hexed ermine
#

Yea

#

It should work

fallow moat
hexed ermine
#

It is

fallow moat
#

No, when io first load in it says 0, not the like 800 it should

hexed ermine
#

Try loading in again

#

These scripts, as they are in this channel, should be working.

fallow moat
#
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
#
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)