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

How to return multiple instances of a table?

So, right now it's a joint question. I wanted to know if you can use the comma's to make the local equal to a certain thing. For example:

local HeadshotDmg, LimbDmg, TorsoDmg = multipleArray(Settings.Damage, 3)

If so, my next question would be how would I end up doing this? This is what I got so far:

function multipleArray(array, amount)
	local values = {}
	for i, v in pairs(array) do
		print(array[i])
		-- Probably somewhere for the return over here
	end
end

3 comments

0
  1. Yes, that can be accomplished, and can be done. 2. What is the use of the Variable 'values' and 'amount' if your not using them in the code? Also, for that kind of situation, you would need to use the 'return' keyword. Document on 'Return': http://wiki.roblox.com/index.php?title=Return#Using_Return Lastly..
0

..Lastly, why in the second code, are you using 'array[i]'? It is not necessary with that kind of 'for' loop; the Variable 'v' specify's the Current Child that is in the 'Table'.

0

The thing about it is that whenever I try to return anything (may it be v), it sets to nil. The array[i] part and values table were parts that I did use before while testing what could work or not. When it returns 'v' for the first time, it will not send anything else back.

1 answer

✓ Accepted answer

What you're looking for is called a tuple you can make a function return a tuple by just returning multiple values, separated by commas, and or semicolons.

function multipleArray(array)
	return array[1],array[2],array[3]
	 --You can also use the 'unpack' function
end

local tab = {3,2,1}

local a,b,c = multipleArray(tab)

--a is now 3
--b is now 2
--c is now 1

I'm not sure how you want to use this to your advantage but here!

1 comment

0

I found that this works best if you use the unpack function after inserting the values in a table. Thanks! Also, this was a localscript taking things from a huge array in a module script so this was to make sorting out certain values easier.

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