Mobile devices leave before save is finished?

I’m running into a weird situation. This only seems to occur on mobile devices. But when they are in the lobby and change some type of value, for instance what unit is selected, it doesn’t seem to save it.

  • This happens when they are in the lobby,
  • Then changed the selected units,
  • They get teleported to a Private Server
  • Before they changed anything it doesn’t load.

Load Data if any:

local selectedData = {}
for _, npcItem in pairs(SelectedUnits:GetChildren()) do
	table.insert(selectedData, {id = npcItem.Name, name = npcItem:FindFirstChild("Name").Value, level = npcItem:FindFirstChild("Level").Value})
end

Save Data entry:

if SelectedUnits then
local selectedData = {}
for _, npcItem in pairs(SelectedUnits:GetChildren()) do
	table.insert(selectedData, {
		id = npcItem.Name,
		name = npcItem:FindFirstChild("Name").Value,
		level = npcItem:FindFirstChild("Level").Value
	})
end
local tries = 0	
local success
repeat
	tries = tries + 1
	success = pcall(function()
		SelectedUnitsStore:SetAsync(player.UserId, selectedData)
		--print("SelectedUnits Saved")
		print("Saved for: "..player.Name.. " userid: "..player.UserId.." --------")
	end)
	if not success then wait(1) end
until tries == 3 or success 
if not success then
	warn("Cannot save data for player: "..player.Name.. " userId: "..player.UserId)
else
	-- DATA WAS SAVED
end
end

However, on my pc, when I log into that alt account and try it on there it works perfectly fine. I get no errors, when I print the table of units selected before they leave during the SaveData function it prints all of them properly.

I save during these instances:
BindToClose or Player is leaving.

**Edit if I wait long enough for the auto-save to kick in while they are in the lobby it saves correctly…

3 Likes

Try using :BindToClose() function when saving, it’ll hold any device to actually save all the data and only then close the app.

Hope this helps! :hidere:

1 Like

Thanks for the response, I already have a BindToClose() function though. This still happens when there are multiple players ingame.

1 Like

Well, if it happens only when there’s more than 1 player, I think a problem isn’t with saving but assigning these values to correct players

1 Like

Yeah I think I figured out the issue. Or at least a weird work around. Seems that when I add a wait(1) before loading in the data it solves the issue.

Now when the player equips a new unit, joins a reserved Server, it loads in properly after waiting 1 second. I think what’s happening is when the player leaves, they join the reserved server before it fully finishes saving their data.