Rooms entity not pathfinding correctly

I am making a rooms game, and the entity isn’t pathfinding correctly.
it sometimes gets stuck in corners. What it’s supposed to do is go to each of the places that it’s supposed to, in order.
Here is my room generation script that also adds the places to the _G table.

local lobby = workspace["The Lobby"]
_G.rooms_exist = {workspace["The Lobby"].RoomDoor}
local savenumber = 0
local savenumbertext = 2
local roomprototypes = {workspace.RoomPrototype1, workspace.RoomPrototype2}
local nextroom = nil
local room1initial = workspace.RoomPrototype1:Clone()
local room1 = workspace.RoomPrototype1
local room2 = workspace.RoomPrototype2
local room3 = workspace.RoomPrototype3
local room_A499 = workspace.RoomA499
room1initial.Parent = workspace
room1initial:PivotTo(CFrame.new(lobby.RoomDoor.Position))
room1initial.RoomPlate.SurfaceGui.TextLabel.Text = "A-"..tostring(savenumbertext)
savenumbertext = savenumbertext + 1
savenumber = savenumber + 1
local lastroomdoor = room1initial.RoomDoor
for placedoors = 1, 499 do
	local randomInteger = math.random(1, 3)
	if randomInteger == 1 then
		nextroom = room1
		
	elseif randomInteger == 2 then
		nextroom = room2
		
	else
		nextroom = room3
		
	end
	if savenumber == 498 then
		nextroom = room_A499
	end
	_G.roomclone = nextroom:Clone()
	_G.roomclone.Parent = workspace
	_G.roomclone:PivotTo(CFrame.new(lastroomdoor.Position))
	_G.roomclone.RoomPlate.SurfaceGui.TextLabel.Text = "A-"..tostring(savenumbertext)
	
	for i, v in pairs(_G.roomclone:GetChildren()) do
		for i, v in pairs(_G.roomclone:GetChildren()) do
			if v:IsA("Part") and v.Name == "RoomDoor" then
				table.insert(_G.rooms_exist, v)
			end
		end
		
	end
	
	lastroomdoor = _G.roomclone.RoomDoor
	savenumbertext = savenumbertext + 1
	savenumber = savenumber + 1
end
for i, v in pairs(_G.rooms_exist) do
	print(v)
end

and here is my script that manages door opening and entity spawning and pathfinding


task.wait(1)
for i, v:Part in pairs(workspace:GetDescendants()) do
	if v.Name == "RoomDoor" and v:IsA "Part" then
		v.Transparency = 0
		v.CanCollide = true
		v.ProximityPrompt.Triggered:connect(function(sus)
			print("Door opened")
			v.Transparency = 0.5
			v.CanCollide = false
			function spawnEntity()
				print("SusSpawn")
				local clone = workspace["A-20"]:Clone()
				clone.Parent = workspace
				clone:PivotTo(workspace["The Lobby"].EntitySpawn.CFrame)
				for i, v in pairs(_G.rooms_exist) do
					clone.Humanoid:MoveTo(v.Position) 
					clone.Humanoid.MoveToFinished:Wait()
				end
			end
			local WillEntitySpawn = math.random(1, 5)
			if WillEntitySpawn == 1 then
				spawnEntity()
			end
		end
		)
	end
end

here is a video:

for some reason it only gets stuck when the next room is a corner room

1 Like

I would recommend watching this video by @GnomeCode:

Rush Monster AI

Just store the current room the player is in, put an invisible part in the middle of each room and get the monster to pathfind starting at the room behind the players invisible part to the next one using moveto()

The entity should start a few rooms behind the player. How would I do this?

Whenever the player enters a new room increment a number on the player by 1. Each rooms invisible part will be named 1-100 or however many rooms you have. So you can get the room behind the player by looking at their value and finding a part say 2 numbers less then that and setting the monster there

Would this work with multiple invisible parts in each room for more precise pathfinding?

You’d need to have some more complex code, for example instead you’d have folders with those parts for each room but yeah should work