Datastore script not working

  1. Is it supposed to work?
  2. Does it have any mistakes?
  3. How can I improve it? (Please don’t say anything about UpdateAsync, lol)

It prints successfully for loading and saving, but actually doesn’t save/load (idk if it is not saving or simply not loading).

local DataStoreService = game:GetService("DataStoreService")
local saveHighscore = DataStoreService:GetDataStore("SaveHighscore")

game.Players.PlayerAdded:Connect(function(player)

    local leaderstats = Instance.new("Folder",player)
    leaderstats.Name = "leaderstats"

    local highscore = Instance.new("NumberValue",leaderstats)
    highscore.Name = "Highscore"

    local loadHighscore
    local success,failure = pcall(function()
        loadHighscore = saveHighscore:GetAsync(player.UserId.."-highscore")
    end)
    if success then
        highscore.Value = loadHighscore
        print("Loaded highscore successfully")
    else
        print("Failed to load highscore")
        warn(failure)
    end

end)

game.Players.PlayerRemoving:Connect(function(player)

    local success,failure = pcall(function()
        saveHighscore:SetAsync(player.UserId.."-highscore",player.leaderstats.Highscore.Value)
    end)
    if success then
        print("Saved highscore successfully")
    else
        print("Failed to save highscore")
        warn(failure)
    end

end)

game:BindToClose(function()

    for _,player in pairs(game.Players:GetChildren()) do
        local success,failure = pcall(function()
            saveHighscore:SetAsync(player.UserId.."-highscore",player.leaderstats.Highscore.Value)
        end)
        if success then
            print("Saved highscore successfully")
        else
            print("Failed to save highscore")
            warn(failure)
        end
    end

end)
1 Like

Also this game is singleplayer, so is there a point in doing PlayerRemoving since when a player leaves the server it will anyways shut down? Or am I wrong?

1 Like

Hello, how are you doing?

Did you publish your game, then went to Game Settings > Security > Enable Studio Acess to API Services and turn it on?
Also, you can just write a single function for both BindToClose and PlayerRemoving.

local function SaveData(player)
    local success,failure = pcall(function()
        saveHighscore:SetAsync(player.UserId.."-highscore",player.leaderstats.Highscore.Value)
    end)
    if success then
        print("Saved highscore successfully")
    else
        print("Failed to save highscore")
        warn(failure)
    end
end)

local gameClosing = false

game.Players.PlayerRemoving:Connect(function(player)

 wait()
 if not gameClosing then
   SaveData(player)
 end

end)

game:BindToClose(function()

 gameClosing = true
 for _,player in pairs(game.Players:GetChildren()) do
    SaveData(player)
 end

end)
1 Like

I don’t know if this is accurate, but i did some tests and when i’m in a server alone and i leave sometimes the server delays a bit to close, or it doesn’t activates what i wanted to.
So for me, i always keep both BindToClose and PlayerRemoving.

Okay I haven’t tried the script yet, but I have a few questions just to understand it.
1.

What is gameClosing?

What is the wait() for?

Also, I just realized, would it be better doing game.Players:GetPlayers() instead?

Yes, the Players:GetChildren() isn’t really reliable. Use that instead.

1 Like

Alright, I opened a new baseplate and tested it, it worked. That specific game had issues with saving for some reason. I’ll move my game to another one. Thank you for the help.

1 Like

No problem! Hope you could figure out the issue for you’re script!

1 Like