How to prevent my script(s) from breaking during loading errors?

I’m not sure if this is an error on my part or ROBLOX, but very rarely there’s a huge error in my game where it won’t load right; for example, when I open up the console, it takes a few minutes to load, but prior to that, it’s just blank. After a few minutes it will just blast all the output it didn’t show before.

The big issue that would happen is during this, if players joined, their stats would be “created” (meaning they would show up in the leaderboard, due to players who didn’t have this issue existing prior), but wouldn’t have any values - like this.

When I checked output out, it gave me a “Player does not exist in data model” error which was catastrophic in one script, as the error prevented player stats from loading which affected other aspects of the game as well.

Here’s the (shortened) code, which is in a generic leaderstats script in ServerScriptService (or whatever they renamed it to):

function create(typ, parent, name, value, plr) --- simplies creating different values for stats
  ---- type of value, where its being parented, name of it, value of it, plr meaning does it save/load?
	local new = Instance.new(typ)
	new.Parent = parent
	new.Name = name
	if plr ~= nil and parent ~= plr then
		local data
		local success, errormsg = pcall(function()
			data = game_Data:GetAsync(plr.UserId.."-"..name)
		end)	
		if success then
			print(name.." successfully loaded in for "..plr.Name)
			if data ~= nil and data > 0 then
				value = data
			end
		else
			print(name.." failed to load for "..plr.Name)
			warn(errormsg)
		end
	end
	if value ~= nil then
		new.Value = value	
	end
	return new
end

function PlayerAdded(plr)
print(plr.Name.." Joined")
local stats = create("IntValue",plr,"leaderstats",0)
local cash = create("IntValue",stats,"Cash",100,plr)
local obj = create("ObjectValue",plr,"tycoons",nil)
end

players.PlayerAdded:Connect(PlayerAdded);
for _,Player in next,players:GetPlayers()do
	task.spawn(PlayerAdded,Player)
end;

I talked to a developer who works for me about this, and he thinks it’s something to do with the client not connecting to the server right; there have been some issues where when someone picks a team in my game, they will be in the initial spawn room for upwards of a minute (usually no more than 5-10 seconds) before the remote event that handles team changing successfully completes the task and re-teams the player. For some players, they are stuck there until they leave and come back, but it’s a rare glitch. Their stats will also still spawn in as well.

Originally I was thinking of running a pcall in a loop (run the loop until the pcall returns successful), before I went with this at his suggestion:

function PlayerAdded(plr)
if not players:WaitForChild(plr.Name,60) then warn("player won't load correctly!?") return; end;
end

That doesn’t solve the issue of what will happen to players who don’t load in correctly after a minute. I was thinking of making it teleport the failed to load player into the game again.

AFAIK this is a super rare glitch but I’m not exactly sure how often it happens since I doubt the players that get affected by this will even bother to try and bring it up to me. It was pretty disheartening to see this occur and then see what was 10 people trying to play my game all leave one after another due to this bug.

What is causing this? Is this something more on my end or is it on ROBLOX’s end and I don’t have the proper protections against it?

1 Like

Here’s another piece of information:

--- this is inside a local script
local SecondsInDay = 1440;
local OffsetSeconds = 0;
local notXbox = true;

UserInputService.InputBegan:Connect(function(Input,GameProcessed) if GameProcessed then return; end;
	local UserInputType = Input.UserInputType;
	if UserInputType == Enum.UserInputType.Gamepad1 then
		notXbox = false;
	end;
end);

while notXbox do
    local Tick = tick();
    local SecondsOfDay = (os.time()+OffsetSeconds)%SecondsInDay;
    local HoursOfDay = SecondsOfDay/SecondsInHour;
    if ClockTime ~= HoursOfDay then
        ClockTime = HoursOfDay;
        NewSecondTick = tick();
        Lighting.ClockTime = ClockTime;
    else
        Lighting.ClockTime = (SecondsOfDay+Tick-NewSecondTick)/SecondsInHour;
    end;
    wait();
end;

Following script was a real-time day/night script in a local script; it caused issues with Xbox clients where it would freeze their game, and they would have to force quit the ROBLOX app in order to get out of my game. I documented this issue here.

Could this be doing a similar effect and causing this issue?