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

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

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

2 comments

0

First variable in for loop is always a number. Add ",v in pairs" or ",v in ipairs" to get the wanted value.

0

gui[i].Visible = true

2 answers

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.

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)

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