Gameloop breaks, pcall returns nothing

We’ve been experiencing issues with the game loop breaking since our game’s inception. The breaking is extremely inconsistent and nearly impossible to reproduce. Using print statements, we pinpointed the area of the game loop that’s causing issues (it didn’t throw errors). We then wrapped the function causing the problems in a pcall, yet the thread’s execution would still stop indefinitely with no return from the call. I’ve attached the code from the problematic areas below as well as the console after the error occurred.

--Calling the problematic function
print('cleared taggers')
print('killing everyone')
local success, errorMessage = pcall(KillEveryone)
if not success then
	warn(errorMessage)
end
print('everyone killed')
--Returning
local PlayerIsReady = function(plr)
	return plr:FindFirstChild("AllPlayerEnteredStatsAdded") ~= nil
end
function KillEveryone()
	local players = game.Players:GetChildren() -- I know this should be :GetPlayers(), showing how it was
	for i=1,#players do
		if players[i] ~= nil then
			if PlayerIsReady(players[i]) and players[i].IsWillingToPlay.Value then
				if players[i].Character then
					players[i]:LoadCharacter()
				end
			end
		end
	end
end

Console
Screen Shot 2020-05-18 at 12.34.27 AM

Are you using :WaitForChild() anywhere? WaitForChild can halt execution without throwing an error, even if encapsulated in a pcall.

I’m not, all of the code that executes between the last print statement and the error occurring is included above

Ah! I think I’ve got it. Your for loop isn’t incrementing.
It should be:
for i=1, #players, 1 do
not
for i=1, #players do

The code works 99% of the time, but something odd happens occasionally that breaks it. If that was the issue, the code would never run. I’m pretty sure the increment argument is optional in a for loop and defaults to 1

It appears you’re correct – looks like I’ve learned something new about for loops today.
Considering the code works fine most of the time, that signals to me that there’s an edge case.
The following pop into mind:
– Player leaves/joins while code is running
– There is a race condition
I’ll have another look at this later after thinking about it some more to see if I come up with anything.

1 Like

Yeah, definitely an obscure edge case. Even more obscure that no errors are printed and it stops the thread. I’d think the checks I have in the code would catch someone leaving/joining. And thanks, let me know if you come up with anything.

If I had to hazard a guess, I’d say that LoadCharacter() is yielding forever for some reason. According to the wiki page, LoadCharacter() is a yielding function.

Searching it on the devforums, it seems like other people have had issues with LoadCharacter yielding.

Perhaps try putting LoadCharacter in a coroutine/fastspawn?

2 Likes

Are you positive that you are calling KillEveryone()? It doesn’t look like you’ve declared the function before the pcall line.

1 Like

I didn’t put the code in order, sorry. The function is definitely defined before it’s called.

Thanks for this information, I’ll definitely look into what you’ve sent. Sounds like it could be it!