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

How do I read multiple values coming from a function?

Asked by
NsNidPL 41
6 years ago

WorldToScreenPoint returns a Vector3 and a boolean. However, I can't read the boolean in any way I tried. How do I read it?

0
For multiple return values you list the variable ie local vec, val = camera:WorldToScreenPoint(pos) User#5423 17 — 6y

1 answer

Log in to vote
1
Answered by
Avigant 2374 Moderation Voter Community Moderator
6 years ago

To receive multiple return values, use multiple assignment:

local function getFirstThreeNumbers()
    return 1, 2, 3
end

local first, second, third = getFirstThreeNumbers()

Extra return results will be discarded:

local function getFirstNumbers()
    return 1, 2, 3, 4
end

local first, second, third = getFirstNumbers() -- Fourth return value discarded.
local anotherFirst, anotherSecond, anotherThird, anotherFourth = getFirstNumbers()
local moreFirst = getFirstNumbers()

Extra variables in multiple assignment will get nil:

local function getFirstNumbers()
    return 1, 2, 3, 4
end

local first, second, third, fourth, fifth, sixth = getFirstNumbers()
print(fifth) --> nil
print(sixth) --> nil
Ad

Answer this question