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

unpack() not working when it goes into a text value?

Asked by 4 years ago
Edited 4 years ago

So I'm coding a security scanner where it sends the tools a player has to a gui.

Yesterday I finished coding it so that instead of going straight into a text value it just prints it and I thought i'd make it go into a text value tomorrow.

I finished coding it today but now when it goes into a text value it only has the first text value.

for i = 1,#c do
    if c[i]:IsA("Tool") then
        if i == #c then
            table.insert(backpack, c[i].Name)
        else
            table.insert(backpack, c[i].Name..", ")
        end
    end
end

--print(unpack(backpack)) -- this works but the one at the bottom won't?

local gui = occupant.PlayerGui.StaffGui
gui.Frame.PlrName.Text = plrname
gui.Frame.AccAge.Text = age
gui.Frame.Tools.Text = unpack(backpack) -- this is where it breaks, only outputs one value

Any help? Thanks!

0
forgot to label line 16, but that's where the issue occours tgarbrechtALTacc 20 — 4y
0
I hope my answer brings you some insight:) Ziffixture 6913 — 4y
0
It does, thanks! tgarbrechtALTacc 20 — 4y

2 answers

Log in to vote
1
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

The unpack() function converts an array into a tuple; the data will be assigned linearly in accordance with the available allocation.

In layman's terms, each element of the tuple requires its own space. Here are a couple of examples to help visualize this:

local arbitraryArray = {"Hello", "world!"}

local firstElement, secondElement = unpack(arbitraryArray)

print(firstElement, secondElement) --> Hello        world!

---------------

local firstElement = unpack(arbitraryArray)

print(firstElement) --> Hello   
--//                (Notice how the second element is truncated)

You're likely attempting to string together the tools' names, which has only resulted in the appearance of one through the truncation explained above. This isn't a complicated solution, however.

You're actually looking for a function that can appropriately concatenate an array's elements into a string. This function is known as table.concat():

Note: This function contains a secondary argument that allows you to declare a separator that will be situated between each element of the array!

local arbitraryArray = {"Hello", "world!"}

print(table.concat(arbitraryArray, " ")) --> Hello world!
Ad
Log in to vote
0
Answered by
Vathriel 510 Moderation Voter
4 years ago

table.unpack and unpack respectively return a variadic format, and not a string like you'd be expecting with unpack(backpack).

You should instead try table.concat

Example:

t = {"A","B","C"}

print(table.concat(t," "))
-->A B C

Answer this question