How Do I Reference An Object Inside A Table Before It's Been Declared?

I would like to reference different room objects before they have been added to a table so that all objects are kind of interconnected.

Roblox will not allow me to do so and I was wondering if there was a way/work around that any of you have come up with.

Here is my code right now:

local Room = require(script.Room)

local Rooms = {}

Rooms.emptyRoom = Room.new(game.ReplicatedStorage.Rooms.EmptyRoom, {["left"] = {Rooms.roomWallLeft, Rooms.roomWallFront, Rooms.roomWallBack}, ["right"] = {Rooms.roomWallRight, Rooms.roomWallFront, Rooms.roomWallBack}, ["front"] = {Rooms.roomWallLeft, Rooms.roomWallRight, Rooms.roomWallBack}, ["back"] = {Rooms.roomWallLeft, Rooms.roomWallFront, Rooms.roomWallRight}})
Rooms.roomWallLeft = Room.new(game.ReplicatedStorage.Rooms.RoomWallLeft, {["left"] = nil, ["right"] = {Rooms.roomWallRight, Rooms.roomWallFront, Rooms.roomWallBack}, ["front"] = {Rooms.roomWallLeft, Rooms.roomWallRight, Rooms.roomWallBack}, ["back"] = {Rooms.roomWallLeft, Rooms.roomWallFront, Rooms.roomWallRight}})
Rooms.roomWallRight = Room.new(game.ReplicatedStorage.Rooms.RoomWallRight, {["left"] = {Rooms.roomWallLeft, Rooms.roomWallFront, Rooms.roomWallBack}, ["right"] = nil, ["front"] = {Rooms.roomWallLeft, Rooms.roomWallRight, Rooms.roomWallBack}, ["back"] = {Rooms.roomWallLeft, Rooms.roomWallFront, Rooms.roomWallRight}})
Rooms.roomWallFront = Room.new(game.ReplicatedStorage.Rooms.RoomWallFront, {["left"] = {Rooms.roomWallLeft, Rooms.roomWallFront, Rooms.roomWallBack}, ["right"] = {Rooms.roomWallRight, Rooms.roomWallFront, Rooms.roomWallBack}, ["front"] = nil, ["back"] = {Rooms.roomWallLeft, Rooms.roomWallFront, Rooms.roomWallRight}})
Rooms.roomWallBack = Room.new(game.ReplicatedStorage.Rooms.RoomWallBack, {["left"] = {Rooms.roomWallLeft, Rooms.roomWallFront, Rooms.roomWallBack}, ["right"] = {Rooms.roomWallRight, Rooms.roomWallFront, Rooms.roomWallBack}, ["front"] = {Rooms.roomWallLeft, Rooms.roomWallRight, Rooms.roomWallBack}, ["back"] = nil})

return Rooms

The room is a pseudo class using a module script since luau doesn’t provide classes normally.

You can’t reference values within a table before the table has been fully initialized. You have to do what you’re currently doing. Just make sure you initialize everything in the correct order.

you must declare it first as it doesnt exist

I think I’ve figured out the solution. I’ll first declare the objects and then add a method to the Room class which allows me to change the connections so that I can declare the rooms first then their connections.

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