Game frequently crashing - Error code 277

My game is facing a problem where it crashes after 20-30 minutes, provided there is a high player count in the server.

I believe it could be attributed to the strong use of pathfinding service in the game, so I will paste the pathfinding script below. This pathfinding script is used to make the monsters chase after the player. However, because there are so many monsters, its likely that after time its causing the game to crash.

Any help is appreciated! Thanks!

Game link: [UPD!] Monster Hunters Simulator - Roblox

Pathfinding script:

local myHuman = script.Parent:WaitForChild("Humanoid")
local myRoot = script.Parent:WaitForChild("HumanoidRootPart")
local head = script.Parent:WaitForChild("Head")
local lowerTorso = script.Parent:WaitForChild("LowerTorso")

local grab = script.Parent:WaitForChild("Grab")
local grabAnim = myHuman:LoadAnimation(grab)
grabAnim.Priority = Enum.AnimationPriority.Action

local grabSound = head:WaitForChild("Attack")

local clone = script.Parent:Clone()

local gameSettings = require(game:GetService("ServerScriptService"):WaitForChild("Modules"):WaitForChild("GameSettings"))

local damageIdo = gameSettings["EnemyInfo"][script.Parent.Name]["Damage"]

function findPath(target)
	local path = game:GetService("PathfindingService"):CreatePath()
	path:ComputeAsync(myRoot.Position,target.Position)
	local waypoints = path:GetWaypoints()

	if path.Status == Enum.PathStatus.Success then
		for i, waypoint in ipairs(waypoints) do
			myHuman:MoveTo(waypoint.Position)

			if (myRoot.Position - target.Position).magnitude < 6 then
				-- stop moving if within 10 studs of target
				attack(target)
				break
			end
			if (myRoot.Position - waypoints[1].Position).magnitude > 20 then

				findPath(target)
				break
			end
		end
	end
end

function attack(target)
	if (myRoot.Position - target.Position).magnitude < 5 then
		grabAnim:Play()
		grabSound:Play()
		if target.Parent ~= nil then
			target.Parent.Humanoid:TakeDamage(damageIdo)
		end
		wait(1)
	end
end

function findPlayer()
	for _, v in ipairs(game.Players:GetChildren()) do
		local plrPosition = v.Character.HumanoidRootPart.Position
		if (myRoot.Position - plrPosition).magnitude < 75 then
			return v.Character.HumanoidRootPart
		end
	end
end

function main()
	local target = findPlayer()
	if target then
		myHuman.WalkSpeed = 14.75
		findPath(target)
	end

	-- Add random jump behavior
	local jump_probability = 0.05 -- Adjust this value to change the frequency of jumps

	if math.random() < jump_probability then
		myHuman.Jump = true
	end
end

while wait(0.05) do
	main()
end

I found this article from another website saying:

If you’re experiencing error code 277 whilst playing Roblox, you almost certainly have a problem with your network. In other words, the internet or data connection to your computer, phone, or tablet is not working well enough to sustain a connection to Roblox.

1 Like

Yeah, the thing is that when it crashes, it affects every single player in the server. If it were a network issue, wouldn’t it only disconnect the player experiencing the issue?

What is the server memory usage before the crash when the game starts and after 15 minutes?

How do you check server memory usage? Or do you mean client memory usage?

Go to the logs in the F9 menu, click server and then MemoryServer.

Wow I never knew that tab existed in the console. But now I think I may have found the problem, it cant be normal for a script to use 230MB of memory…?

Sever memory usage started at around 500MB, now its 1000MB after around 10 minutes.

Someone a while back had a similar issue, and it was because the pathfinding was done very far from the origin of the world (0,0,0).

Well the pathfinding is taking place max 100 studs from the origin.

However after looking at the memory usage, the pathfinding scripts are not using much memory.

Compared to game handler using 500MB, and increasing significantly.

It’s probably not the pathfinding scripts, but whatever the GameHandler is doing.

Yeah seems that way. I’ll paste the game handler script, as I’m not exactly sure what could be causing the huge memory usage. Although it might be to do with the coroutines being used.

local SSS = game:GetService("ServerScriptService")
local RS = game:GetService("ReplicatedStorage")
local SS = game:GetService("ServerStorage")

local gameSettings = require(SSS:WaitForChild("Modules"):WaitForChild("GameSettings"))
local startGameBE = SSS:WaitForChild("GameEvents"):WaitForChild("StartGame")
local gameRE = RS:WaitForChild("GameRemotes"):WaitForChild("GameRemote")
local ExpRE = RS:WaitForChild("GameRemotes"):WaitForChild("ExpRemote")
local CoinRemote = RS:WaitForChild("GameRemotes"):WaitForChild("CoinRemote")
local DiamondRemote = RS:WaitForChild("GameRemotes"):WaitForChild("DiamondRemote")
local capturesInfo = require(SSS:WaitForChild("Modules"):WaitForChild("capturesInfo"))
local captureNoticiationRE = RS:WaitForChild("InventoryRemotes"):WaitForChild("captureNotificationRE")

local dataModule = require(SSS:WaitForChild("Data"):WaitForChild("DataModule"))

local startWave = 15

local bayInformation = {
	["Bay1"] = {
		["Players"] = {},
		["Wave"] = nil,
		["Location"] = "Bay1",
		["ActiveSection"] = 1,
	},
	["Bay2"] = {
		["Players"] = {},
		["Wave"] = nil,
		["Location"] = "Bay2",
		["ActiveSection"] = 1,
	},
	["Bay3"] = {
		["Players"] = {},
		["Wave"] = nil,
		["Location"] = "Bay3",
		["ActiveSection"] = 1,
	},
	["Bay4"] = {
		["Players"] = {},
		["Wave"] = nil,
		["Location"] = "Bay4",
		["ActiveSection"] = 1,
	},
	["Bay5"] = {
		["Players"] = {},
		["Wave"] = nil,
		["Location"] = "Bay5",
		["ActiveSection"] = 1,
	},
	["Bay6"] = {
		["Players"] = {},
		["Wave"] = nil,
		["Location"] = "Bay6",
		["ActiveSection"] = 1,
	},
}

--false = availble, true = taken
local bayAvailability = {false, false, false, false, false}

local WaveThemes = game:GetService("ServerStorage"):WaitForChild("WaveThemes")
local WaveThemesTbl = {
	WaveThemes["NatureWave"],
	WaveThemes["BarbieWave"],
	WaveThemes["SnowWave"],
	WaveThemes["FireWave"],	
}

local wavesPerTheme = 15


local function initialiseGame(bay, plrsInGame)
	local gameThread = coroutine.wrap(function()

		local loadWave
		bayInformation[bay]["ActiveSection"] = 1
		local enemiesInWave = {}
		local maxEnemies = gameSettings["Wave"..tostring(bayInformation[bay]["Wave"])]["NumEnemiesToKill"]
		local wave = bayInformation[bay]["Wave"]
		local enemiesSpawnedInGroup = 0
		local enemiesKilledInWave = 0
		local gameEnded = false
		
		
		
		local function endGame()
			gameEnded = true
			local waveDisplay1 = workspace.Bays[bay].Section1.WaveDisplay
			local waveDisplay2 = workspace.Bays[bay].Section2.WaveDisplay
			local section = workspace.Bays[bay]["Section"..tostring(bayInformation[bay]["ActiveSection"])]
			waveDisplay1.SurfaceGui.WaveNum.Text = "1"
			waveDisplay2.SurfaceGui.WaveNum.Text = "1"
			for _, v in ipairs(section.Enemies:GetChildren()) do
				v:Destroy()
			end

			bayAvailability[tonumber(string.match(bay, "%d+"))] = false
			bayInformation[bay]["Players"] = {}
			bayInformation[bay]["Wave"] = nil
			bayInformation[bay]["ActiveSection"] = 1
		end

		local function spawnEnemies(enemiesToSpawn)
			local section = workspace["Bays"][bay]["Section"..tostring(bayInformation[bay]["ActiveSection"])]
			if #section.Enemies:GetChildren() ~= 0 then
				for _, v in ipairs(section.Enemies:GetChildren()) do
					v:Destroy()
				end
			end
			
			local function setWaveTheme(waveThemeNumber)
				local waveModel = WaveThemesTbl[waveThemeNumber]
				local waveModelClone = waveModel:Clone()
				local activeSectionModel = section
				waveModelClone.Name = activeSectionModel.Name
				activeSectionModel.Name = "old"
				local cframeToTP = activeSectionModel.PrimaryPart.CFrame
				waveModelClone.Parent = activeSectionModel.Parent

				waveModelClone:SetPrimaryPartCFrame(cframeToTP)
				activeSectionModel:Destroy()
				section = workspace["Bays"][bay]["Section"..tostring(bayInformation[bay]["ActiveSection"])]
			end
			
			if wave/wavesPerTheme <= 1 then
				setWaveTheme(1)
			elseif wave/wavesPerTheme <=2 then
				setWaveTheme(2)
			elseif wave/wavesPerTheme <=3 then
				setWaveTheme(3)
			elseif wave/wavesPerTheme <=4 then
				setWaveTheme(4)
			end
			
			local waveDisplay = section.WaveDisplay
			waveDisplay.SurfaceGui.WaveNum.Text = tostring(wave)
			
			
			for _, enemyType in pairs (enemiesToSpawn) do	
				if gameEnded == false then
					local enemyToSpawn = enemyType["Name"]
					local startingNum = 1
					for i = startingNum, enemyType["Number"], 1 do
						if gameEnded == false then
							if enemiesSpawnedInGroup < 6 then
								enemiesSpawnedInGroup = enemiesSpawnedInGroup + 1
							else 
								enemiesSpawnedInGroup = 1
								wait(2)
							end

							local enemyToClone = SS:WaitForChild("EnemiesStorage"):FindFirstChild(enemyType["Name"])
							local clonedEnemy = enemyToClone:Clone()


							local cframeToTP = section.EnemySpawns["Spawn"..tostring(enemiesSpawnedInGroup)].CFrame

							clonedEnemy.Parent = section.Enemies
							clonedEnemy.HumanoidRootPart.CFrame = cframeToTP
							local currentWave = "Wave"..tostring(bayInformation[bay]["Wave"])

							local cloneHealth = gameSettings["EnemyInfo"][clonedEnemy.Name]["Health"]
							clonedEnemy.Head.BillboardGui.Frame.Health.Text = tostring(cloneHealth).."/"..tostring(cloneHealth)
							clonedEnemy.Humanoid.MaxHealth = cloneHealth
							clonedEnemy.Humanoid.Health = cloneHealth


							table.insert(enemiesInWave, clonedEnemy)
							local debounce = false
							local checkForDeadHumanoid = coroutine.wrap(function()
								local stopCrashing = false
								while stopCrashing == false do
									for _, value in ipairs(enemiesInWave) do
										if value:FindFirstChild("Humanoid") then
											if value.Humanoid.Health <= 0 then
												enemiesKilledInWave = enemiesKilledInWave + 1
												for _, v in ipairs(plrsInGame) do
													--Value False = start game True = Edit Info	
													gameRE:FireClient(game.Players[v], wave, maxEnemies, enemiesKilledInWave)
												end
												local enemyValToRemove = table.find(enemiesInWave, value)
												if enemyValToRemove then

													table.remove(enemiesInWave, enemyValToRemove)
												end

												if enemiesKilledInWave == maxEnemies then
													--wave finished
													debounce = true
													local activeSection = bayInformation[bay]["ActiveSection"]
													if activeSection == 1 then
														bayInformation[bay]["ActiveSection"] = 2
													else
														bayInformation[bay]["ActiveSection"] = 1
													end
													enemiesSpawnedInGroup = 0
													enemiesKilledInWave = 0

													local coinsAwarded = gameSettings["Wave"..tostring(wave)]["Coins"]
													if coinsAwarded then

														for _, v in ipairs(plrsInGame) do
															local plrData = dataModule.ReturnData(game.Players:FindFirstChild(v))
															local coinsMultiplier = plrData["Multipliers"]["Coins"]
															local baseCoinsMult = plrData["Multipliers"]["BaseCoins"]
															local newCoinsAwarded = tostring(math.floor(coinsAwarded*(coinsMultiplier*baseCoinsMult)))
															dataModule.EditData(game.Players[v], "Coins", newCoinsAwarded, "addition")
															CoinRemote:FireClient(game.Players[v], newCoinsAwarded)
														end
													else
														local diamondsAwarded = gameSettings["Wave"..tostring(wave)]["Diamonds"]
														for _, v in ipairs(plrsInGame) do
															local plrData = dataModule.ReturnData(game.Players:FindFirstChild(v))
															local diamondsMultiplier = plrData["Multipliers"]["Diamonds"]
															local baseDiamondsMult = plrData["Multipliers"]["BaseDiamonds"]
															local newDiamondsAwarded = tostring(math.floor(diamondsAwarded*(diamondsMultiplier*baseDiamondsMult)))
															dataModule.EditData(game.Players[v], "Diamonds", newDiamondsAwarded, "addition")
															DiamondRemote:FireClient(game.Players[v], newDiamondsAwarded)
														end
													end
													for _, v in ipairs(plrsInGame) do
														local plrData = dataModule.ReturnData(game.Players[v])
														for _, value in pairs(capturesInfo) do

															if wave == value["WaveToBeat"] and not table.find(plrData["Captures"]["Unequipped"], value["Name"]) and not table.find(plrData["Captures"]["Equipped"], value["Name"]) then
																table.insert(plrData["Captures"]["Unequipped"], value["Name"])
																captureNoticiationRE:FireClient(game.Players[v])

															end
														end
													end



													if gameSettings["Wave"..tostring(wave)]["Coins"] then
														local Coin = SS:WaitForChild("GameItems"):WaitForChild("CoinEmmiter"):Clone()
														Coin.Position = Vector3.new(value.HumanoidRootPart.Position.X, 1, value.HumanoidRootPart.Position.Z)
														Coin.Parent = workspace
														value:Destroy()
														local pe = Coin.ParticleEmitter
														local COIN_SOUND = game:GetService("ReplicatedStorage"):WaitForChild("Sounds"):WaitForChild("CoinSoundEffect")
														local coinSound = COIN_SOUND:Clone()
														coinSound.Parent = Coin
														coinSound:Play()

														pe.Enabled = true
														wait(.25)
														pe.Enabled = false
														wait(1.5)
													else
														local Diamond = SS:WaitForChild("GameItems"):WaitForChild("DiamondEmmiter"):Clone()
														Diamond.Position = Vector3.new(value.HumanoidRootPart.Position.X, 1, value.HumanoidRootPart.Position.Z)
														Diamond.Parent = workspace
														value:Destroy()
														local pe = Diamond.ParticleEmitter
														local Diamond_SOUND = game:GetService("ReplicatedStorage"):WaitForChild("Sounds"):WaitForChild("DiamondSound")
														local diamondSound = Diamond_SOUND:Clone()
														diamondSound.Parent = Diamond
														diamondSound:Play()

														pe.Enabled = true
														wait(.25)
														pe.Enabled = false
														wait(1.5)
													end
													local maxWave = gameSettings["MaxWave"]
													if wave == maxWave then
														for _, v in ipairs(plrsInGame) do
															gameRE:FireClient(game.Players[v], wave+1, nil, nil, nil,true)
															game.Players[v].Character.HumanoidRootPart.CFrame = workspace.SpawnPosition.CFrame
														end				
													else
														wave = wave + 1
														stopCrashing = true
														loadWave("Wave"..tostring(wave))	
													end

												else
													value:Destroy()
												end
											end
										end
										for _, v in ipairs(plrsInGame) do
											local plr = game.Players:FindFirstChild(v)
											if plr then
												local char = plr.Character
												local humanoid = char:FindFirstChild("Humanoid")
												if humanoid.Health == 0 then
													print("plrDied")
													local plrToRemove = table.find(plrsInGame, v)
													if plrToRemove then

														gameRE:FireClient(plr, nil, nil, nil, true)
														table.remove(plrsInGame, plrToRemove)

														if #plrsInGame == 0 then
															stopCrashing = true
															endGame()
														end
													end	

												end
											end
										end
									end
									--check if any players leave
									game:GetService("Players").PlayerRemoving:Connect(function(player)
										local isPlrInGame = table.find(plrsInGame, player.Name)
										if isPlrInGame then
											table.remove(plrsInGame, isPlrInGame)
											if #plrsInGame == 0  then
												stopCrashing = true
												endGame()
											end
										end
									end)
									wait(.25)
								end
							end)
							checkForDeadHumanoid()
						else
							for _, v in ipairs(section:GetChildren()) do
								v:Destroy()
							end
						end	
					end	
				end				
			end
		end

		loadWave = function(waveToLoad)	
			if waveToLoad then
				

				local activeSection = bayInformation[bay]["ActiveSection"]
				local section = workspace["Bays"][bay]["Section"..tostring(activeSection)]

				for i = 1, #plrsInGame, 1 do
					local posToTeleport = section.PlayerSpawns["Spawn"..tostring(i)]
					game.Players[plrsInGame[i]].Character.HumanoidRootPart.CFrame = CFrame.new(posToTeleport.Position)
				end
				maxEnemies = gameSettings[waveToLoad]["NumEnemiesToKill"]
				for _, v in ipairs(plrsInGame) do
					local player = game.Players:FindFirstChild(v)

					--Value False = start game True = Edit Info

					gameRE:FireClient(player, wave, maxEnemies, nil, nil, nil,bay, bayInformation[bay]["ActiveSection"])
					
					
				end
				coroutine.resume(coroutine.create(spawnEnemies),gameSettings[waveToLoad]["Enemies"])
			end
		end


		local waveToLoad = "Wave"..tostring(bayInformation[bay]["Wave"])
		loadWave(waveToLoad)
	end)
	gameThread()
end

startGameBE.Event:Connect(function(plrsInGame, waveToStartAt)
	local bayFound = false
	for number, v in ipairs(bayAvailability) do
		if bayFound == false then
			if v == false then
				bayFound = true
				bayAvailability[number] = true
				local bayChosen = "Bay"..tostring(number)
				for index, plr in ipairs(plrsInGame) do
					table.insert(bayInformation["Bay"..tostring(number)]["Players"], plr)
				end

				bayInformation["Bay"..tostring(number)]["Wave"] = waveToStartAt

				local maxEnemies = gameSettings["Wave"..tostring(bayInformation[bayChosen]["Wave"])]["NumEnemiesToKill"]
				local wave = bayInformation[bayChosen]["Wave"]

				initialiseGame(bayChosen, plrsInGame)
			end
		end	
	end
end)

I was thinking of that. It seems like you’re using coroutine.wrap, which can be changed to task.spawn. It might also be possible that you’re not clearing one of the many tables involved in the game thread.

I’ve never used task.spawn before, but ill give it a shot.

Solution was to modify the game handler so that the “checkForDeadHumanoid” coroutine didn’t execute so many times, and to yield it once it was no longer required.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.