How do I fix this error?

Hello! So I am making a random room generator and I gotten this error " Script ‘ServerScriptService.Server.Room’, Line 22 - function GetRandom (x4997) - Studio - Room:22
and Script ‘ServerScriptService.Server.Room’, Line 35 - function Generate" I`ve tried for 2 hours trying to fix this on youtube and devfourm.

script:

local room = {}
room.info = require(script.RoomInfo)
room.LastTurnDirection = nil
room.random = Random.new()

function room.GetRandom(prevRoom)
	local possibleRooms = workspace.Rooms:GetChildren()
	local randomRoom = possibleRooms[room.random:NextInteger(1, #possibleRooms)]
	
	
	
	
	
	local direction = room.info[randomRoom.Name]["Direction"]
	local hasStairs = room.info[randomRoom.Name]["Stairs"]
	local prevHadStairs = room.info[prevRoom.Name]["Stairs"]
	
	if (prevRoom.Name == prevRoom.Name)
		or (direction and direction == room.LastTurnDirection)
		or (hasStairs and prevHadStairs)
	then
		return room.GetRandom(prevRoom)
	else
		if direction then
			room.LastTurnDirection = direction
		end
		
		return randomRoom
	end
	
	
end

function room.Generate(prevRoom)
	local randomRoom = room.GetRandom(prevRoom)
	local newRoom = randomRoom:Clone()
	
	newRoom.PrimaryPart = newRoom.Entrance
	newRoom:PivotTo(prevRoom.Exit.CFrame)
	
	newRoom.Parent = workspace.GeneratedRooms
	
	return newRoom
end

return room

whats the error say? rather than just the line its on

1 Like

‘ServerScriptService.Server.Room’, Line 22 - function GetRandom (x4997) - Studio - Room:22
and Script ‘ServerScriptService.Server.Room’, Line 35 - function Generate

Your function is calling itself, we call this recursion and it is a form of infinite loop. The inciting line is

if (prevRoom.Name == prevRoom.Name) -- this will always be true
    -- so this will always run, and thus repeat forever
    return room.GetRandom(prevRoom)

You need to break this somehow, I’m willing to bet object == same object is a bug and you mean to use prevRoom.Name == randomRoom.Name

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