For i, v in pairs - V is printing/representing all instances

I’m trying to create a table that includes a list of part coming from a folder. For one reason or the other my code ends up printing both instances when calling on Index value [1].

Code:

local PlayerFolder = workspace:WaitForChild(“TestPlayers”)
InRoundTable = {}

function Playerlist()
for i, v in game.Workspace.TestPlayers:GetChildren() do
if v.Name then
table.insert(InRoundTable, 1, v)
print(InRoundTable[1], v)
end
end

end

Playerlist()

Any explanation or help would be greatly appreciated

in your code, you have a table (aka GetChildren() with returns a table of Instances)
the for loop is looking through all the values within that table

You are basically inserting a value under a key, which is the Instance, since the key is assigned as 1, you are printing the key 1 which is the Instance

You should be using table[index] = Instance (in your code: it should be inRoundTable[i] = v,
Howerver, you can just do:

table.insert(Table, Instance)

as for the print, the print is a Tuple which means it takes in any value, so you can do:

function DoSomething(...) -- ... is the Tuple
   print(...) -- prints Tuple
end

DoSomething("Hello", 12) -- "Hello 12"

Since you assigned the 1 key as the Instance, it will print both the Key Value and the Instance, They print the same, Because They are same as they are the same Instance under that iteration.

basically, in ever iteration in this loop, it looks through a new Instance within that Array.

Try this:

local playerFolder = workspace:WaitForChild("TestPlayers")
local inRoundTable = {}

local function playerList()
	for i,v in pairs(playerFolder:GetChildren()) do
		if v.Name then --??
			inRoundTable[i] = v
			print(inRoundTable[i])
		end
	end
end

By the way,
if you are attempting to get a table of all players in the server, (not exactly sure if thats your goal…) you can use game.Players:GetPlayers(). This inbuilt function will return a table of all players in the server.

Quick note

What does if v.Name do?
Every instance in the explorer has a name… so this condition will always be true.

Thats not really what they are asking?

Am I missing something…?

From what it seems, they are asking how to insert the children of PlayerFolder into a table. But now that I look at it, Instance:GetChildren() already does this, which was already in the original code…

I feel very dumb and confused now. :sweat:

This is Basically what they are asking: (the words in Bold)

I’m confused. what is this supposed to do? You constantly overwrite the InRoundTable[1] resulting in printing the same object twice in the print statement under it. Maybe you intended to have it overwrite based on index, in which case you would use

table.insert(InRoundTable, i, v); 

or

InRoundTable[i] = v;

. I also noticed that you didn’t use pairs or ipairs, which is important or the for won’t iterate.

this isnt true, and thats not how pairs and ipairs work, the for loop can still iterate without them.

I Recommend Reading about it:

  • You should use ipairs like this: ipairs(game.Workspace.TestPlayers:GetChildren()) (instead of game.Workspace.TestPlayers:GetChildren())

Edit:

I didn’t know that, thanks! I was wrong originally: not using ipairs is perfectly fine.

  • This code doesn’t make sense: if v.Name then. Maybe you meant to check if the name was equal to something or if the instance exists? To check equality do if v.Name == "Example" then. To check if an instance exists do if v then (you don’t need to check if the instance exists in this case).

  • table.insert(InRoundTable, 1, v) Having the 1 there causes the insert to happen at the front. This means the new item is added at index 1. To have the print statement in the loop print both, change the insert to: table.insert(InRoundTable, v). This adds the element to the end of the list.

Essentially i’m looping through them because I’ll eventually have to check for a bool value within the player.

FWIW: You don’t need to use ipairs anymore. What OP had was generalised iteration which follows the behaviour of ipairs first (numerically contiguous iteration) before pairs (unordered). They’re still fine to plug tables directly into the iterator part of a for loop.

Luau Lang - Syntax: Generalised iteration & Luau RFC: Generalised iteration

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.