How can I get multiple numbers through a for i,v loop?

Hello,
I’m working on an intro for my game and I am hoping to get the value of all folders within the player. Let me explain a little better if possible. So, a player can have up to 3 max slots, my goal is to get all of the current slots a player has and making them visible. Here’s the code that I have for it.

script.Parent.introComplete.Changed:Connect(function()
	for _,v in pairs(game.Players:FindFirstChild(player.Name):GetChildren()) do
		if v.ClassName == "Folder" and v.Name == "StorageReader" then
		filenum = v:FindFirstChild("ID").Value
		end
	end
	
	saves:TweenPosition(UDim2.new(0.5,0,0.5,0),"In", "Bounce", 1)
	
	for i,v in pairs(saves.saves.slots:GetChildren()) do
		if v.ClassName == "Frame" and v.Name ~= "create" then
			v.Parent:FindFirstChild("slot"..filenum).Visible = true
		end
	end
end)

The problem is, it’s only making the final slot visible. My goal is to not have to make up to 3 values and then use elseif statements. So, how could I make this happen? And if you have questions comment and I’m more than happy to respond!

EDIT: filenum is a variable, local firenum = 0

It’s kind of hard for me to understand what you’re trying to say;

but I’m assuming that what you’re trying to do is if the player has a children(s) called “StorageReader”, you’d want the filenumber value of those, and then change the visibility of the slot with that filenumber. (correct me if I’m wrong)

What I would do is put all filenumbers inside a table, and then loop through it and so on.

script.Parent.introComplete.Changed:Connect(function()
	local fileNumbers = {}
	for _,v in pairs(game.Players:FindFirstChild(player.Name):GetChildren()) do
		if v.ClassName == "Folder" and v.Name == "StorageReader" then
		table.insert(filenumbers, v:FindFirstChild("ID").Value)
		end
	end
	
	saves:TweenPosition(UDim2.new(0.5,0,0.5,0),"In", "Bounce", 1)
	
    for _,v in pairs(fileNumbers) do
	for _,x in pairs(saves.saves.slots:GetChildren()) do
		if x.ClassName == "Frame" and x.Name ~= "create" then
			x.Parent:FindFirstChild("slot" .. v).Visible = true
		end
	end
    end
end)

I had a feeling there would be a misunderstanding with what my goal was, but, you got it spot on! I see what you did and I’ll test it out! Thank you for your response :slight_smile:.

In addition to that, I think you shouldn’t do two in pairs loops. Instead use only one:

for i, v in pairs(saves.saves.slots:GetChildren()) do
	if v.ClassName == "Frame" and v.Name ~= "create" then
        if filenumbers[i] ~= nil then
		    v.Parent:FindFirstChild("slot" .. filenumbers[i]).Visible = true
        end
	end
end
2 Likes

Figured out that there was something slightly off with me having two loops :joy:, but couldn’t get my head around it.

When I try this I get an error which is 15:56:03.894 - Players.eizaray1234.PlayerGui.Gui.ClientIntro:17: attempt to concatenate string with nil. Not sure why it’s giving me an error.

Because it’s nil

Change
for i, v in pairs(saves.saves.slots:GetChildren()) do
	if v.ClassName == "Frame" and v.Name ~= "create" then
		v.Parent:FindFirstChild("slot" .. filenumbers[i]).Visible = true
	end
end
to
for i, v in pairs(saves.saves.slots:GetChildren()) do
	if v.ClassName == "Frame" and v.Name ~= "create" then
		v.Parent:FindFirstChild("slot" .. v).Visible = true
	end
end

Well probably the value that is at i index doesn’t exist in the table so just add an if statement to see if the value is not nil.