Datastore not working without this code

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?

Well you can debounce that tbh, I didn’t realise that it would save twice.

local alreadySaving={}
local function OnPlayerRemoving(plr)
	if table.find(alreadySaving,plr)then return end;table.insert(alreadySaving,plr);
	if plr:WaitForChild("Loaded").Value then
		Data.Save(plr)
	end
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.