Hello!
Context: I have a data saving function when the player leaves and it is meant to save a few variables in RouteData (the airplane routes like New York to Chicago and Sydney to Dubai type of routes) from at most 5 different slots. I have added multiple print statements of what the data should be and this is what it prints:
Slots table: ▶ {...} - Server - DataStore:107
17:51:03.486 ----- - Server - DataStore:112
17:51:03.486 Processing slot: 1 - Server - DataStore:113
17:51:03.486 slotData found for slot: 1 - Server - DataStore:121
17:51:03.486 Routes found for slot: 1 - Server - DataStore:130
17:51:03.486 Found route: MQL-SYD in slot: 1 - Server - DataStore:133
17:51:03.486 Route data: - Server - DataStore:140
17:51:03.486 Distance: 838 - Server - DataStore:141
17:51:03.486 TotalFlightTime: 0.9305652490478677 - Server - DataStore:142
17:51:03.486 FlightTimeLeft: 1.669551072859881 - Server - DataStore:143
17:51:03.486 LastUpdateTime: 1777790494 - Server - DataStore:144
17:51:03.487 Status: En-Route - Server - DataStore:145
17:51:03.487 Progress: 0.2333861328930011 - Server - DataStore:146
17:51:03.487 PhaseDurations: ▶ {...} - Server - DataStore:147
17:51:03.487 nil - Server - RouteSimulation:77 -- Disregard this line
17:51:03.487 nil - Server - RouteSimulation:79 -- Disregard this line too
17:51:03.487 Display data returned: - Server - DataStore:165
17:51:03.487 Status: En-Route - Server - DataStore:166
17:51:03.487 Progress: 0.2333861328930011 - Server - DataStore:167
17:51:03.487 TimeLeft: 1H 40M - Server - DataStore:168
17:51:03.487 Updated route values: - Server - DataStore:188
17:51:03.487 DistanceRemaining: 642.422420635665 - Server - DataStore:189
17:51:03.487 DistanceTravelled: 195.57757936433495 - Server - DataStore:190
17:51:03.487 Progress: 0.2333861328930011 - Server - DataStore:191
17:51:03.487 Status: En-Route - Server - DataStore:192
17:51:03.487 FlightTimeLeft: 1.669551072859881 - Server - DataStore:193
Problem: The loop only runs one slot and then stops looping, but sometimes it runs all 5 loop iterations, i can not confirm the reason why. Though these moments may impact that: Whenever I play a save file or whenever I don’t and so i just quickly join and leave. They might work differently
If someone can identify any reason to why this happens, it would be appreciated!
Server Script Snippet:
game.Players.PlayerRemoving:Connect(function(player)
local userId = player.UserId
print("PlayerRemoving fired for:", player.Name, userId)
local slots = PlayerData[userId]
if not slots then
warn("No PlayerData found for player:", player.Name, userId)
return
end
local function isEmpty(dict)
if not dict then
return true
end
for _ in pairs(dict) do
return false
end
return true
end
print("Slots table:", slots)
for slot = 1, 5 do
local slotData = slots[slot]
print("-----")
print("Processing slot:", slot)
local loadSuccess, loadError = pcall(function()
if not slotData then
warn("slotData is nil for slot:", slot)
return
end
print("slotData found for slot:", slot)
local routesData = slotData.Routes
if not routesData then
warn("Routes table is nil for slot:", slot)
elseif isEmpty(routesData) then
print("Routes table is empty for slot:", slot)
else
print("Routes found for slot:", slot)
for routeId, route in pairs(routesData) do
print("Found route:", routeId, "in slot:", slot)
if not route then
warn("Route is nil for routeId:", routeId, "slot:", slot)
continue
end
print("Route data:")
print(" Distance:", route.Distance)
print(" TotalFlightTime:", route.TotalFlightTime)
print(" FlightTimeLeft:", route.FlightTimeLeft)
print(" LastUpdateTime:", route.LastUpdateTime)
print(" Status:", route.Status)
print(" Progress:", route.Progress)
print(" PhaseDurations:", route.PhaseDurations)
local success, dataOrError = pcall(function()
return RouteSim.GetDisplayData(route)
end)
if not success then
warn("RouteSim.GetDisplayData failed for route:", routeId, "slot:", slot, dataOrError)
continue
end
local data = dataOrError
if not data then
warn("GetDisplayData returned nil for route:", routeId, "slot:", slot)
continue
end
print("Display data returned:")
print(" Status:", data.Status)
print(" Progress:", data.Progress)
print(" TimeLeft:", data.TimeLeft)
if not route.Distance then
warn("route.Distance is nil for route:", routeId, "slot:", slot)
continue
end
if data.Progress == nil then
warn("data.Progress is nil for route:", routeId, "slot:", slot)
continue
end
local distanceRemaining = route.Distance - (route.Distance * data.Progress)
local distanceTravelled = route.Distance - distanceRemaining
route.DistanceRemaining = distanceRemaining
route.DistanceTravelled = distanceTravelled
route.Progress = data.Progress
route.Status = data.Status
print("Updated route values:")
print(" DistanceRemaining:", route.DistanceRemaining)
print(" DistanceTravelled:", route.DistanceTravelled)
print(" Progress:", route.Progress)
print(" Status:", route.Status)
print(" FlightTimeLeft:", route.FlightTimeLeft)
end
end
local saveSuccess, saveError = pcall(function()
saveSlot(player, slot, slotData)
end)
if saveSuccess then
print("Successfully saved slot:", slot, "for player:", player.Name)
else
warn("Failed to save slot:", slot, "for player:", player.Name, saveError)
end
end)
if not loadSuccess then
warn("Slot processing crashed for slot:", slot, loadError)
else
print("Successfully loaded slot:", slot, "for player:", player.Name)
end
end
PlayerData[userId] = nil
print("Finished saving all slots for:", player.Name)
end)