Just so you know ipairs/pairs is not needed in Luau and indexing values in table instead of having dirrect referance is bad for perfomance and writing speed overall.
Also please avoid using annonimous functions unless you are creating a closure.
Your code could’ve been a bit simplier
for _, player in ipairs(Players:GetPlayers()) do
task.spawn(saveDataOnShutdown,Player)
end
Also if you want already made implementation of safe data saving you can use ProfileStore
In your case it was erroring as since you either reached limit or its just a random error there so try doing 4 more attempts to save data.
I also dont understand why are you trying to do that?
That overcomplication of logic and deoptimizzation to me…
The switch table you are making could be made a bit more optimized and easier:
local ServerFactors={
[1]=function():()--Reserved
local function TeleportPlayer(P)
P.Content.Value=true
Services.TeleportService:Teleport(game.PlaceId,P,{["SoftShutdown"]={true,"Removal"}})
end
Services.PlayersService.PlayerAdded:Connect(function(P)
task.wait(DelayFactor)
TeleportPlayer(P) DelayFactor/=2
end)
for _, P in pairs(Services.PlayersService:GetPlayers()) do
TeleportPlayer(P) task.wait(DelayFactor)
DelayFactor/=2
end
end;
[2]=function()--Standart
game:BindToClose(function():()
task.spawn(function()
if Services.RunService:IsStudio() then
warn("Soft Shutdown: System Works Only In Actual Game")
return
end
workspace:SetAttribute("ShuttingDown",true)
local UITick=os.clock()
local ReservedCode=Services.TeleportService:ReserveServer(game.PlaceId)
task.wait(4)
local function TeleportPlayer(P)
if (os.clock()-UITick)<0.5 then
task.wait(1.25)
end
P.Content.Value=true
delay(0.3,function()
Services.TeleportService:TeleportToPrivateServer(game.PlaceId,ReservedCode,{P},nil,{["SoftShutdown"]={true,"Addition"}})
end)
end
Services.PlayersService.PlayerAdded:Connect(function(P)
TeleportPlayer(P)
end)
for _, P in pairs(Services.PlayersService:GetPlayers()) do
TeleportPlayer(P)
end
end)
while #Services.PlayersService:GetPlayers()>0 do
task.wait()
end
end)
end;
[3] = function():()--Default
end;
}
ServerFactors[((game.PrivateServerId~="" and game.PrivateServerOwnerId ~= 0 and 3) or 1) or 2]()