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

DataStore not saving after player leaves ?

Asked by 2 years ago

So basically, I want to save the data, but for some reason, it doesn't save it.

--I just put a part of my code
function generateDataTable(player)
    local CashPosition = game.ReplicatedStorage.PlayerCash:FindFirstChild(player.Name)
    print("1")
    if CashPosition then
        print("2")
        print(CashPosition.Value)
        local dataTable = {
            Cash = CashPosition.Value
        }
        print("3")
        return dataTable
    end
end

function saveDataForPlayer(player)
    print("s")
    local key = generateDataKey(player)
    local data = generateDataTable(player)
    print("Just one more thing !")
    datastore:SetAsync(key, data)
    print("set !")
end
--Oh and there is a PlayerRemoving event that is connected to the SaveDataForPlayer function

Let me explain: the CashPosition is an IntValue with the player's name (each player has one thing) because I first thought that data was not saved, because the leaderstats folder was already deleted from the game. So I created an IntValue that I put in ReplicatedStorage to use this value if the other one in leaderstats gets deleted.

So far it is printing up to ("Just one more thing !") so it does everything except the SetAsync function so I don't really know what to do in order for it to SAVE.

0
Are you receiving any errors in the Output window? COUNTYL1MITS 312 — 2y
0
nope denbra37 34 — 2y
0
are you calling the function? MOREHOURSOFFUN 104 — 2y
0
Yep, it prints everything up to the GetAsync() function which quite annoys me, and there are no errors in the output denbra37 34 — 2y

2 answers

Log in to vote
1
Answered by 2 years ago
Edited 2 years ago

The server is probably shutting down before it gets a chance to save all your data. You can use the game:BindToClose() function similarly to how you would connect to any other event, and it runs right before the server shuts down

You should keep the PlayerRemoving connection so that a player's data is saved as soon as they leave, and when the server shuts down, it should save the data of everyone still in the server to make sure the data is saved

local function serverClosing()
    for i, player in pairs(game.Players:GetPlayers()) do
        saveDataForPlayer(player)
    end
end

game:BindToClose(serverClosing)
0
Whoah, I wanted to make something like this but I didn't know this thing existed ! Thank you so much ! denbra37 34 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

You got API Services on? If you don't, turn them on.

0
Yep, and I published the game denbra37 34 — 2y

Answer this question