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

Creating multiple variables on the same line?

Asked by 5 years ago
Edited 5 years ago

I have noticed that some scripters create variables on the same lines as each other. Example:

local one,two,three = false,true,1 --I put random variables in

now that makes sense (at least, I think). My question is, is the order they are defined in the same order they are listed? In otherwords, is it the equivilent to this:

local one = false
local two = true
local three = 1

Also There are some cases where declaring multiple variables on the same line DON'T make so much sense, an example of this would be part of raycasting (Source:www.robloxdev.com/articles/Making-a-ray-casting-laser-gun-in-Roblox)

local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)

What does the part mean, and what does the position mean? And how does roblox match the variables to their definitions?

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

Return

The reason for multiple variables being assigned in the FindPartOnRay in one line is because said method returns. It returns more than one item, so you can assign each variable in the order you return them in. Take this for example:

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

local one, two, three = OneTwoAndThree()
print(one, two, three) --> 1 2 3

It prints 1, 2, and 3, and in order because that's the order in how it was returned.

0
Okay, so then I was right when I said that the order it is declared matches the order it is defined right? And also, what are the two return values for part and position? DominousSyndicate 41 — 5y
0
Yes. Part is the part the ray hit, and position I'm not too sure. I don't work with ray casting a lot! User#19524 175 — 5y
Ad

Answer this question