Need Help on a sort of "Stack Overflow" Bug I cannot manage to fix

Hello everyone,

I have been encountering a bug in my game for some time now that I cannot manage to fix at all, so I have come to the forum for some potential insight.

Basically, randomly whenever I playtest my game, my function repeatedly has a “stack overflow” sort of bug which yields the code of my game. This function handles time in the game which limits the amount of time players spend on each stage of the round, resetting the time whenever everyone passes or kills players that did not complete the stage in enough time.

I am not able to manually recreate this bug, it randomly and rarely happens during normal gameplay. I’ve included a screenshot of the output when this bug occurred, it repeats that print statement repeatedly yielding the script.

Also, the didNotChooseObbyYet value can only be changed in one other function, and that function is not running frequently

Note this function is being ran on a coroutine.

I am not sure why this bug is happening, especially since I have the :Wait() function incorporated

If anyone can please provide me any insight, tips, feedback, anything of that nature on why this bug might be happening for the below-listed code, I would be grateful and appreciate it very much.

function gametime()
	while GameOn.Value do
		if didNotChooseObbyYet.Value == true then
			didNotChooseObbyYet.Changed:Wait()
		elseif didNotChooseObbyYet.Value == false then 
			task.wait()
			print("line 1087: GameOn: "..tostring(GameOn.Value).." didNotChooseObbyYet: "..tostring(didNotChooseObbyYet.Value))
			FrontBlocker:MoveTo(CurrentPlatform.Position)
			for i = 0, STAGE_TIME do

				-- In order to prevent any killings in the lobby, we will stop the loop if the game suddenly ends
				if GameOn.Value == false then
					-- Let client know Game is over.
					gametimeLeftEvent:FireAllClients(100)
					break
				end

				-- If all players finish the obby, end the round
				if #playerInObby:GetChildren() == 0 then
					
					for _, playerName in ipairs(readyPlayers) do
						if Players:FindFirstChild(playerName).UserId ~= RecentWinnerId.Value then
							local Value = Instance.new("StringValue")
							Value.Name = playerName
							Value.Value = playerName
							Value.Parent = playerInObby
						end
					end

					-- We let the clients know the round is over and hide the timer
					gametimeLeftEvent:FireAllClients(100)
					break
				end

				-- This If statement activates once the countdown is over
				if i == STAGE_TIME then

					for key, playerName in pairs(readyPlayers) do -- This for loop will go through all players who have not finished the obby yet
						-- If a player is found, we kill them
						if playerInObby:FindFirstChild(playerName) then
							Players:FindFirstChild(playerName).Character:FindFirstChildOfClass("Humanoid").Health = 0
							PlayerDiedEvent:FireAllClients(playerName)
						end					
					end

					-- This for loop will iterate through all players in the game
					for key, playerName in pairs(readyPlayers) do

						-- This If statement will again insert the player in playerInObby if won (The winner will not play that obby)
						playerInObby:ClearAllChildren()
						if Players:FindFirstChild(playerName).UserId ~= RecentWinnerId.Value then
							local Value = Instance.new("StringValue")
							Value.Name = playerName
							Value.Value = playerName
							Value.Parent = playerInObby
							print("Created a player value at 906 for "..Value.Value)
						end
					end

					-- We let the clients know the round is over
					gametimeLeftEvent:FireAllClients(100)
					break
				end

				-- Inform the clients how much time is left in the game
				gametimeLeftEvent:FireAllClients(STAGE_TIME - i, i)

				task.wait(1)
			end
		else
			print("Bug. Else statement reached. didnotchoose and GameOn Value:"..tostring(didNotChooseObbyYet.Value)..(tostring(GameOn.Value)))
		end
	end
end

Here is an image of the console output when this bug happened, the second photo is before I added a task.wait() before the print statement:


1 Like

Does STAGE_TIME ever change? If it is for whatever reason == 0 then it will skip the for loop. It’s written in all caps as a constant so I assume it doesn’t change.

No, it does not, the bug starts from the if #playerInObby == 0 statement