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

How would I get the 3rd parameter in table.insert which is v.Parent to print?

Asked by 5 years ago
local tbl = {}

for i,v in pairs(game.Workspace:GetChildren()) do
    if v.Name == 'Value' then
        table.insert(tbl,v.Value,v.Name,v.Parent) -- v.Parent wont print
    end


end

wait(1)
for i,v in pairs(tbl) do
    print(i,v) -- I dont understand do I add in another paramater?
end

1 answer

Log in to vote
1
Answered by 5 years ago

You seem to not understand how table.insert works. You're assuming table.insert can take a tuple and insert it all into a given table.

Argument order of table.insert is table.insert(t, i, v) where [i] = v.

So if v is absent, it is assumed that v is i and v is inserted at #t + 1 (the very end position)

When you do supply the second optional argument, everything at and after position i will be shifted 1 to the right, and v is in position i

-- Code example
t = {9, 8, 7};
table.insert(t, 2, "test");
print(table.concat(t, ", "));

The output would be

9, test, 8, 7

Because the string was inserted at position 2. insert doesn't just replace.

local tbl = {}

for _, v in ipairs(game.Workspace:GetChildren()) do
    if v.Name == 'Value' then
        table.insert(tbl, v.Value)
        table.insert(tbl, v.Name)
        table.insert(tbl, v.Parent)
    end
end

wait(1)
for i, v in ipairs(tbl) do
    print(i, v)
end

Ad

Answer this question