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
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.
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?