#leaderstats not saving

1 messages · Page 1 of 1 (latest)

bright crow
#

pls help, I tried so many tutorials its still not saving
code:


local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("LeaderStatSaveRacks")
local ds2 = DataStore:GetDataStore("LeaderStatSaveXPs")

game.Players.PlayerAdded:Connect(function(player)
    local leader = Instance.new("Folder")
    leader.Name = "leaderstats"
    leader.Parent = player

    local racks = Instance.new("IntValue")
    racks.Name = "Racks"
    racks.Parent = leader

    local xp = Instance.new("IntValue")
    xp.Name = "XP"
    xp.Parent = leader

    local success1, savedRacks = pcall(function()
        return ds:GetAsync(tostring(player.UserId))
    end)
    if success1 and savedRacks ~= nil then
        racks.Value = savedRacks
    else
        racks.Value = startAmount
        warn("Could not load Racks data")
    end

    local success2, savedXP = pcall(function()
        return ds2:GetAsync(tostring(player.UserId))
    end)
    if success2 and savedXP ~= nil then
        xp.Value = savedXP
    else
        xp.Value = startAmount
        warn("Could not load XP data")
    end
end)

local function savePlayerData(player)
    local leader = player:FindFirstChild("leaderstats")
    if not leader then return end

    local racks = leader:FindFirstChild("Racks")
    local xp = leader:FindFirstChild("XP")

    if racks and xp then
        pcall(function()
            ds:SetAsync(tostring(player.UserId), racks.Value)
            ds2:SetAsync(tostring(player.UserId), xp.Value)
        end)
    end
end

game.Players.PlayerRemoving:Connect(savePlayerData)

game:BindToClose(function()
    for _, player in ipairs(game.Players:GetPlayers()) do
        savePlayerData(player)
    end
end)
potent plinth
#

you're probably changing the values on the client so only 0's are saved. very common beginner mistake.

bright crow
#

this finally makes sense thank u bro