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.
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)
You got API Services on? If you don't, turn them on.