Datastore not working without this code

You don’t need tostring, but yes, that would be fine.

If you’re already using tostring, do that as numbers and strings are seperate datatypes, so the save files would be different.

isn’t it good practise to ensure it’s the correct data type since Datastore keys will be converted to a string anywway

I don’t think I’ve ever heard that, but even if it does, you don’t need to do that operation for DataStore anyway. Interesting info though.

wait since set async takes time/ yields would it be better if I did

    for _, plr in Players:GetPlayers() do
        task.spawn(function()
               local Data = nil -- get the player data
               DataStore:SetAsync(tostring(plr.UserId), Data)
        end)
    end
end)

this way all the data saving for all players woulud happen simulatneously and make the server wait less time

it won’t let me reply that is odd.

I Solved it here:

what’s wrong with my one

game:BindToClose(function()
    for _, plr in G.Plrs:GetPlayers() do
		OnPlayerRemoving(plr)
	end
end)

On player removing is the same function that fires when a plalyer leavse the game

the problem now is when I leave the game in studio the PlayerRemoving and the BindToClose run
so it’s trying to save my data twice

if not game:GetService'RunService':IsStudio()then
    -- BindToClose inside here
end

I thought the player leaving doesn’t have time to run in studio so would this make it so data doesn’t save at all in studio?

Well saving data really just matters that it saves in-game mainly and on studio that it doesn’t so any data you was testing with wouldn’t be saved but I don’t know it should be working tho.

This looks good?

local function OnPlayerRemoving(plr)
	if plr:WaitForChild("Loaded").Value then
		Data.Save(plr)
	end
end




local function BindPlayerRemoving()
	G.Plrs.PlayerRemoving:Connect(OnPlayerRemoving)
end




local function ServerClosing()
	if not G.RunService:IsStudio() then
		local AllPlayers = G.Plrs:GetPlayers()
		
		for _, plr in AllPlayers do
		OnPlayerRemoving(plr)
		end
		
		if #AllPlayers > 0 then
			G.TeleportService:TeleportAsync(game.PlaceId, AllPlayers)
		end
	end
end

if you don’t wait for data to save inside “BindToClose” then the game will close before it even saves.

it doesn’t wait for the threads to finish?#

well yeah because your skipping the wait on OnPlayerRemoving function by using it inside a task.spawn function.

in this yt tutorial he uses task.spawn https://www.youtube.com/watch?v=H-cDbjd5-bs&ab_channel=GnomeCode
so I guess he might be wrong
It’s just hes pretty good at scripting so I thought I could trust him

Yeah he got it wrong because if you don’t wait for the function/thread to complete inside BindToClose function then the game will close causing all scripts/threads to suspend and not run anymore meaning it wouldn’t save anything.

still though this code woulud mean that if you tried it in game the game closing and the playerremoving would both fire

it’s not supposed to do that as I know.

it automatically only runs one of theM?