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

How to return multiple instances of a table?

Asked by
IXLKIDDO 110
9 years ago

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
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.. TheeDeathCaster 2368 — 9y
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'. TheeDeathCaster 2368 — 9y
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. IXLKIDDO 110 — 9y

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

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!

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. IXLKIDDO 110 — 9y
Ad

Answer this question