Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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!

3 comments

0

forgot to label line 16, but that's where the issue occours

0

I hope my answer brings you some insight:)

0

It does, thanks!

2 answers

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!

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

This discussion is preserved as it was. New answers, comments, and votes are closed.