Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

DataStore not saving after player leaves ?

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.

4 comments

0

Are you receiving any errors in the Output window?

0

nope

0

are you calling the function?

0

Yep, it prints everything up to the GetAsync() function which quite annoys me, and there are no errors in the output

2 answers

✓ Accepted answer

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)

1 comment

0

Whoah, I wanted to make something like this but I didn't know this thing existed ! Thank you so much !

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

1 comment

0

Yep, and I published the game

This discussion is preserved as it was. New answers, comments, and votes are closed.