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.
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.
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’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.
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.
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.