Script thread dies (on LoadCharacter?)

Hi all, this is actually a platform bug report, but I can’t post in that category, so this will have to do instead. I had to search to find that apparently this was intentional, but I really do not understand the logic. Surely there should be a way for me as a developer to actually submit bugs, right?

Regardless, here is the actual problem that I’m having. I have a round-based game where very custom characters are loaded (and reloaded often). At the end of the game, all the characters have the teams reset and then are returned to the spawn point. This works about 99.9% of the time. Unfortunately, about .1% of the time, the thread just dies. There is no exception or anything, the thread just dies. Other threads keep running and doing stuff, but the one that is supposed to reset the characters and basically drive the rounds forward just disappears.

Here is the relevant server code, which runs at the end of the round (and rarely but sometimes kills the running thread):


local Teams = game.Teams
local TeamSpectators = Teams["Spectators"]

function ResetToSpectator(player)
	if (player ~= nil) then
		if (player.Parent ~= nil) then
			player.Team = TeamSpectators;
			player.TeamColor = TeamSpectators.TeamColor
			player:LoadCharacter()
		end
	end
end
	
function ResetAllToSpectators()	
	local players = Players:GetPlayers()
	for index, player in pairs(players) do
		if (player ~= nil) then
			if (player.Parent ~= nil) then
				local success, err = pcall(function()
					ResetToSpectator(player)
				end)
				if (success == false) then
					warn("Reset to Spectator Error:")
					warn(err)
				end
			end
		end
	end
end

local resetToSpectatorsSuccess, resetToSpectatorsError = pcall(function()
	ResetAllToSpectators()
end)
if (resetToSpectatorsSuccess == false) then
	warn("Error Resetting to Spectators:")
	warn(resetToSpectatorsError)
end
1 Like

Note that I am assuming the script dies in LoadCharacter, but I’m not actually sure. I’m adding some logging to try to catch this, but because this issue happens so infrequently, it is hard to be at the server when this happens. (I have no doubt I’ll be able to narrow it down and verify, but it will just take me some more iterations).

Note, also, all the characters at this point are still alive. Another developer suggested I kill each character first, then wait, then respawn. I have not tried this and it seems like a huge hack, but I might give it a go if it turns out to be necessary. I noticed on some other posts that people were in fact doing something just like this. And another suggestion to wait between each reset, which I don’t like at all and seems unnecessary.

Another option would be to spawn off another thread to basically reset all the characters, so if that spawned thread died, at least the game would keep going (though some unknown number of players might still not be spectators yet!) And the additional even more extreme option to do better than this would be to spawn off another thread for each character to reset them. :frowning: Not really a fan of this either, but maybe it would work.

Anyways, if anyone has run into something similar or has any ideas, please let me know! It would be greatly appreciated.

sjg

This seems like a quick fix but
Cant you do something like set
_G.Player_USERID
to a value which means something like

The Number Code

1 = Changing To Spectator
2 = Spectator
3 = Changing to Playing
4 = Playing

and in a OnCharacterAdded so when the character spawns it checks if the number is a “Changing” value and if it is then just start that process you wrote since it didn’t finish correctly.

1 Like

Yeah I think something like this could work. I was considering adding a ‘monitoring thread’, which just kind of keeps track of what is supposed to happen in that primary thread, and if a certain step doesn’t happen after a large period of time, assume the entire thread has died, and then just respawn that primary thread again (starting at the appropriate point). Kind of a similar idea.

sjg

What your thinking could work but what if it kills both threads. The monitor first then the main. Are you going to have a monitor for that monitor. Or have the main thread monitor the monitor

Sure, I suppose this could happen. Guess we will see! :wink:

Did you ever fix this? I am having this exact issue, but not so infrequently. I’ve narrowed it down to these lines of code (forgive me I’m on mobile)

print(“Respawning”,Player)
Player:LoadCharacter()
print(“Respawned”)

It says respawning and the thread dies after that.

Unfortunately I didn’t ‘fix’ it, I just worked around the Roblox thread crash. It still looks to be a Roblox bug. In the end, it looks like it just would kill the thread, so I basically moved my LoadCharacter to its own thread, so if it died, at least my spawning thread would continue. My final code looks like this:

function ReloadPlayerCharacters(playersToReload)

for index, player in pairs(playersToReload) do
	if (player ~= nil) then
		if (player.Parent ~= nil) then
			spawn(function()
				if (player ~= nil) then
					if (player.Parent ~= nil) then
						player:LoadCharacter()
					end
				end
			end)
		end
	end
end

end