#DataStoreService

1 messages · Page 1 of 1 (latest)

rustic geyser
#

For some reason data store saving and loading (at least it prints it), but the value of Int not chaning. Any ideas?

#
local leaderstatsDataStore = DataStoreService:GetDataStore("leaderstatsDataStore")

local function loadPlayerData(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local strength = Instance.new("IntValue")
    strength.Name = "Strength"
    strength.Value = 0
    strength.Parent = leaderstats

    local wins = Instance.new("IntValue")
    wins.Name = "Wins"
    wins.Value = 0 
    wins.Parent = leaderstats

    local success, data = pcall(function()
        return leaderstatsDataStore:GetAsync(tostring(player.UserId))
    end)

    if success and data then
        strength.Value = data.Strength or 0
        wins.Value = data.Wins or 0
        print("Data loaded for " .. player.Name)
    elseif success and not data then
        print("No saved data for " .. player.Name .. ", using default values.")
    else
        warn("Error loading data for " .. player.Name)
    end
end```
#
local function savePlayerData(player)
    local leaderstats = player:FindFirstChild("leaderstats")
    if not leaderstats then return end

    local strength = leaderstats:FindFirstChild("Strength")
    local wins = leaderstats:FindFirstChild("Wins")
    if not (strength and wins) then return end

    local data = {
        Strength = strength.Value,
        Wins = wins.Value
    }

    local success, err = pcall(function()
        leaderstatsDataStore:UpdateAsync(tostring(player.UserId), function()
            return data
        end)
    end)

    if success then
        print("Player data for " .. player.Name .. " successfully saved.")
    else
        warn("Error saving data for " .. player.Name .. ": " .. err)
    end
end

game.Players.PlayerAdded:Connect(loadPlayerData)
game.Players.PlayerRemoving:Connect(savePlayerData)

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

It's one script, I just got limit

merry totem
#

why not print(data) when it loads so u can be sure of your referencing

brave sorrel