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
7 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 — 7y

1 answer

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

To receive multiple return values, use multiple assignment:

1local function getFirstThreeNumbers()
2    return 1, 2, 3
3end
4 
5local first, second, third = getFirstThreeNumbers()

Extra return results will be discarded:

1local function getFirstNumbers()
2    return 1, 2, 3, 4
3end
4 
5local first, second, third = getFirstNumbers() -- Fourth return value discarded.
6local anotherFirst, anotherSecond, anotherThird, anotherFourth = getFirstNumbers()
7local moreFirst = getFirstNumbers()

Extra variables in multiple assignment will get nil:

1local function getFirstNumbers()
2    return 1, 2, 3, 4
3end
4 
5local first, second, third, fourth, fifth, sixth = getFirstNumbers()
6print(fifth) --> nil
7print(sixth) --> nil
Ad

Answer this question