The for loop in my script is bugging out

I’m just as confused as those in the Unofficial Roblox Discord Server, so I’ll attempt to explain this and have it make sense.

So the for loop line, which in this case is line 12, isn’t working.
As you can see Buildings is an existing table, so if I were to loop through it since House1 is a member of the table, it should print House1.Name, which is House1. Instead, nothing happens and it does not print v.Name and instead, I don’t even know.

Output v

1
2

Script v

local Buildings = {
	House1 = {
		Name = "House1";
		Description = "A simple, cozy, small home.";
		Population = 10;
		Price = 2500;
	}
}

print(1)
if Player ~= nil and BuildingType ~= nil then
	print(2)
	for i, v in ipairs(Buildings) do
		print(v.Name)
		if BuildingType == v.Name then
			print("go heyr")
			game.ReplicatedStorage.RequestInfo:FireClient(Player, v.Name, v.Description, v.Price, v.Population)
		end
	end
end

I don’t know if this is an error on Studios end or I’m just being stupid and there’s an obvious solution. Thanks.

You’re looping through the Buildings dictionary however you are trying to access Name as if it were located at Buildings.Name instead of Buildings.House1.Name

If you want to loop through all houses, you can do:

for k, v in pairs(Buildings) do
    -- Loop through the houses within the Buildings dictionary
    for ik, iv in pairs(v) do
        -- Should successfully print the name, do whatever you need to do with it
        print(iv.Name)
    end
end
2 Likes

Thank you. I just tested it and works.

1 Like