Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why isn't my datastore saving? It is not overwriting any values.

Asked by 3 years ago
Edited 3 years ago

I have created a datastore that is supposed to save. It saves a table into the datastore, but the moment I add another value to the datastore, the whole thing does not even save once. I have already tried modifying both values of the datastore before leaving the game and it did not save at all.

By the way, the datastore saves in normal gameplay, but not in play test, and it works whenever I use the server testing thing. How do I make it possible to save in play test with two values?

Here is my code. Please help, I do not understand why it is not saving.

game.Players.PlayerRemoving:Connect(function(plr)
    modded_data = game.DataStoreService:GetDataStore("ModdedData")

    local random_values = plr.Folder

    local stuff = {} -- do the serialization :)

    for i,v in pairs(random_values:GetChildren()) do
        stuff[v.Name] = v.Value
    end
    print(stuff)
    local success, err = pcall(function()
        modded_data:SetAsync(plr.UserId, stuff)
    end)
    if success then
        print('The player data of the user '..plr.Name..' has been saved successfully.')
    elseif err then
        print(err)
    end
end)

2 answers

Log in to vote
0
Answered by 3 years ago

I have resolved the problem and found out that I just need to use the BindToClose function in order to stop the server from shutting down while saving data.

It may have a time limit of 30 seconds, but I feel that is enough.

Ad
Log in to vote
0
Answered by
MattVSNNL 620 Moderation Voter
3 years ago

Here try this, It saves at all times

local ds = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(plr)
    wait()
    local plrkey = "id_"..plr.UserId
    local save1 = plr.leaderstats.Stage -- Change this to your leaderstat name
    local save2 = plr.leaderstats.Coins -- If you only have one leaderstat then remove this

    local GetSaved = ds:GetAsync(plrkey)
    if GetSaved then
        save1.Value = GetSaved[1]
        save2.Value = GetSaved[2]
    else
        local NumberForSaving = {save1.Value, save2.Value}
        ds:GetAsync(plrkey, NumberForSaving)
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    ds:SetAsync("id_"..plr.userId, {plr.leaderstats.Stage.Value, plr.leaderstats.Coins.Value})
end)
0
I did not need the answer... I just wanted to answer my own question, which all you need to do is use the BindToClose function which handles whenever you stop the server. Sorry for the downvote, but I already answered it, and I gave an explanation. Frometa1998 35 — 3y

Answer this question