Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why won't this code run?

Asked by 8 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

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

Log in to vote
0
Answered by
LostPast 253 Moderation Voter
8 years ago

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
Ad

Answer this question