Table always empty?

Hey Devforum, i asked ChatGPT to help me with connecting rooms since i haven’t exactly learned much about that yet. Why is the exits table of every generatedRooms entry always empty? I have confirmed the Exits folder exists, and they’re all Parts.

local function generateRooms()
	generatedRooms = {}

	-- Clear currentRoomFolder
	for _, child in pairs(currentRoomFolder:GetChildren()) do
		child:Destroy()
	end

	-- Simple example: pick unique rooms and create a chain
	for i = 1, NUMBER_OF_ROOMS do
		local availableRooms = basementFolder:GetChildren()
		if #availableRooms == 0 then
			warn("No rooms in basementFolder!")
			break
		end
		local roomTemplate = availableRooms[math.random(1, #availableRooms)]
		
		local roomClone = roomTemplate:Clone()
		assignPrimaryPart(roomClone)
		roomClone.Parent = currentRoomFolder
		positionRoom(roomClone, i) -- Now this will work using SetPrimaryPartCFrame
		roomClone.Name = "Room_" .. i
		addChestPrompts(roomClone)
		
		task.wait()

		-- Prepare exits dictionary
		local exitsFolder = roomClone:FindFirstChild("Exits")
		local exitsDict = {}

		if exitsFolder then
			for _, exitPart in pairs(exitsFolder:GetChildren()) do
				exitsDict[exitPart.Name] = nil
			end
		end
		
		table.insert(generatedRooms, {room = roomClone, exits = exitsDict})
	end

	-- Now link rooms - for example, link Room i's first exit to Room i+1
	for i = 1, NUMBER_OF_ROOMS - 1 do
		local currentRoomData = generatedRooms[i]
		local nextRoomData = generatedRooms[i + 1]

		local exitNames = {}
		for exitName in pairs(currentRoomData.exits) do
			table.insert(exitNames, exitName)
		end

		if #exitNames > 0 then
			-- Link first exit to next room
			print("Linking", currentRoomData.room.Name, "exit", exitNames[1], "to Room_" .. tostring(i + 1))
			currentRoomData.exits[exitNames[1]] = i + 1
		end
	end

	-- Add prompts only for exits that connect to another room
	for i, roomData in ipairs(generatedRooms) do
		for exitName, connectedRoomIndex in pairs(roomData.exits) do
			local exitPart = roomData.room.Exits:FindFirstChild(exitName)
			if connectedRoomIndex then
				-- Create prompt to enter next room
				local prompt = Instance.new("ProximityPrompt")
				prompt.ActionText = "Enter"
				prompt.ObjectText = "Room " .. connectedRoomIndex
				prompt.Parent = exitPart

				prompt.Triggered:Connect(function(player)
					local nextRoomData = generatedRooms[connectedRoomIndex]
					if not nextRoomData then return end

					spawnPlayerAtRandomExit(nextRoomData.room, player)
				end)
			end
			-- No prompt if no connected room
		end
	end
	
	print(generatedRooms)
end
1 Like

I believe it’s because you set the exitparts to nil

exitsDict[exitPart.Name] = nil

When you’re doing this, you are basically setting something to nonexistent. Which is why your tables are empty. What you could do instead is set it to false, that way you could still work with something.

exitsDict[exitPart.Name] = false
2 Likes

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