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

for loop telling me variable is a number value after using GetChildren()?

Asked by 6 years ago

Yeah so I get the children of a GUI and then use a for loop to check the children yet its telling me it cant because its a number value? Since when does :GetChildren() return a number?

local but = script.Parent
local site = script.Web.Value
local ser = but.Parent.SerBar
local url = script.URL

but.MouseButton1Click:connect(function(plr)
    local gui = but.Parent:FindFirstChild(site):GetChildren()
    local all = but.Parent:GetChildren()
    for v = 1 , #all do
        if v.ClassName == "Folder" then
            local gc = v:GetChildren()
            for e = 1, #gc do
                e.Visible = false
            end
        end
    end
    ser.Text = url.Value
    for i = 1, #gui do
        i.Visible = true
    end
end)

This is the Error Players.CrispyBrix.PlayerGui.SurfaceGui.WHITE.Home.Click:10: attempt to index local 'v' (a number value)

Any help is appreciated

0
First variable in for loop is always a number. Add ",v in pairs" or ",v in ipairs" to get the wanted value. Meltdown81 309 — 6y
0
gui[i].Visible = true hellmatic 1523 — 6y

2 answers

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

This is not how you use a numerical loop to access an array. You are using the number in the loop as opposed to using the number to access the element in the array.

Example.

local tbl = {'a', 'b', 'c'}

for i=1, #tbl do
    print(tbl[i])
end

You should have also read the error which explains the issue in your code as looping through tables is a basic part of programming.

Ad
Log in to vote
0
Answered by 6 years ago

Try this

local but = script.Parent
local site = script.Web.Value
local ser = but.Parent.SerBar
local url = script.URL

but.MouseButton1Click:connect(function(plr)
    local gui = but.Parent:FindFirstChild(site):GetChildren()
    local all = but.Parent:GetChildren()
    for i,v in pairs(all) do
        if v.ClassName == "Folder" then
            local gc = v:GetChildren()
            for u,f in pairs(gc) do
                f.Visible = false
            end
        end
    end
    ser.Text = url.Value
    for i,v in pairs(gui) do
        v.Visible = true
    end
end)

Answer this question