Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why won't this code run?

The code refused to run, nothing is printed and nothing is draw to my screen except "hello?" Can anyone please explain why this wont function correctly? It should draw an image and text Label with the correct info as specified in the houseConfig table. Not even i is printed from the for loop, someone please help?

wait(2)
print("hello?")

local houseConfig = {
	
	house1 = {
		Name = "House 1",
		Owner = "Noone",
		Image = "rbxgameasset://Images/download_2",
		Price = 100
	},
	
	house2 = {
		Name = "House 2",
		Owner = "Noone2",
		Image = "rbxgameasset://Images/download_2",
		Price = 200
	}
}


local gui = script.Parent

for i = 1, #houseConfig do
	print(i)
	
	local houseImage = Instance.new('ImageLabel',gui.Frame)  --Draw Image Button
    houseImage.Name = tostring(i.Name)
    houseImage.Size = UDim2.new(0.12,0,1,0)
    houseImage.Position = UDim2.new(10,0,0,i*0.1)
	houseImage.Image = i.Image
	
	local houseName = Instance.new('TextLabel',houseImage) --Draw Text Label
	houseName.Position = UDim2.new(0.5,0,1.1,0)
	print(i.Name)
	houseName.Name = tostring(i.Name)
    houseName.Text = tostring(i.Name)
	houseName.Font = "SourceSansBold"
	houseName.TextColor = BrickColor.White()
	houseName.FontSize = "Size18"
	
end

1 answer

✓ Accepted answer

if you put print(#houseConfig) on line 23 it will print 0 since #houseConfig can not be used on the type of table you are using.

you have to use for v,i in pairs() do instead which searches through the table.

I also changed some of your i's to v's because you were looking for a value of the table.

wait(2)
local c = 0

local houseConfig = {

    house1 = {
        Name = "House 1",
        Owner = "Noone",
        Image = "rbxgameasset://Images/download_2",
        Price = 100
    },

    house2 = {
        Name = "House 2",
        Owner = "Noone2",
        Image = "rbxgameasset://Images/download_2",
        Price = 200
    }
}


local gui = script.Parent

for i,v in pairs(houseConfig) do -- i counts as the index so this will work. v is the table inside of the table.
	c=c+ 1
    print(c)

    local houseImage = Instance.new('ImageLabel',gui.Frame)  --Draw Image Button
    houseImage.Name = tostring(v.Name)
    houseImage.Size = UDim2.new(0.12,0,1,0)
    houseImage.Position = UDim2.new(10,0,0,c*0.1)
    houseImage.Image = v.Image

    local houseName = Instance.new('TextLabel',houseImage) --Draw Text Label
    houseName.Position = UDim2.new(0.5,0,1.1,0)
    print(v.Name)
    houseName.Name = tostring(v.Name)
    houseName.Text = tostring(v.Name)
    houseName.Font = "SourceSansBold"
    houseName.TextColor = BrickColor.White()
    houseName.FontSize = "Size18"

end

This discussion is preserved as it was. New answers, comments, and votes are closed.