GlobalDataStore issues

  1. What do you want to achieve?
    For our datastore to be actually saving and not removing itself out of thin air

  2. What is the issue?
    DataStore Removing statistics of games Played and games Won

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

We are pretty new to DataStores and we did try to look for solutions on the Developer Hub.

Here is our GlobalDataStore which we made for Wins and games Played:

local ds = game:GetService("DataStoreService"):GetGlobalDataStore()

game.Players.PlayerAdded:Connect(function(plr)
    print("Player added:", plr.Name)

    local success, err = pcall(function()
        local plrkey = "id"..plr.UserId
        local saveWins = plr.hiddenstats.Wins
        local savePlayed = plr.hiddenstats.Played

        local savedWins = ds:GetAsync(plrkey)
        if savedWins then
            saveWins.Value = savedWins
        end

        local playedKey = "played"..plr.UserId
        local savedPlayed = ds:GetAsync(playedKey)
        if savedPlayed then
            savePlayed.Value = savedPlayed
        end
    end)

    if not success then
        warn("Error loading player data:", err)
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    print("Player removing:", plr.Name)

    local success, err = pcall(function()
        local plrkey = "id"..plr.UserId
        ds:SetAsync(plrkey, plr.hiddenstats.Wins.Value)

        local playedKey = "played"..plr.UserId
        ds:SetAsync(playedKey, plr.hiddenstats.Played.Value)
    end)

    if not success then
        warn("Error saving player data:", err)
    end
end)

The datastore tried to access the hiddenstats before it was created, so use WaitForChild() to wait for the folder to be created to avoid errors

1 Like

Thanks! im going to try that right now

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.