How are these two code snippets identical? I've never seen this before

I was reading through some free models and I saw this “syntax”. I’ve never seen this before but I managed to figure out what it does. What is this called and why does it work? One thing’s for sure, it’s ugly.

-- What I found
script.Name, script.Parent='Foo', workspace.Terrain
-- What seems to be identical
script.Name = 'Foo'
script.Parent = workspace.Terrain
1 Like

They’re (I think are called) multiple assignments. They’re basically the same in this context but not when using function calls or varargs (...) at the end. If a function call returned multiple values, it assigns to its respect variable in the order when you want to assign them.
For example:

local function c()
    return 0, 1
end
locale a, b = c()
print(a, b) --> 0 1

After reading through the Lua manual something clicked…

It becomes clear what’s going on when I add spaces:
script.Name, script.Parent = "Test", workspace

I get it now…

1 Like

that code is a multiple assignment. It is quite useful when a function returns multiple values or just to shorten the code.

1 Like

@Blockzez @maika_w

I was just confused… In JavaScript you get a different result:

I was used to the comma operator being similar to a semi colon.

It’s true, I had a hard time understanding the comma operator. Tuples are something unique in lua.

In JavaScript (and C), assignment is an experssion not a statement and comma operator evaluates all of operands and discards all but the last of them.

Python? And Lua doesn’t have tuples. Lua’s documentaiton doesn’t even call it a “tuple”.

2 Likes

I don’t know if I understood your point, but there are documentation of tuples. Assuming we are talking about roblox lua, which is now luau.

It’s not called “tuples” despite Roblox documentation saying so. In Lua(u), if a function returns multiple values; they return multiple values; they’re not put together as a single data structure; they’re separate values.

Let me quote the part of @TheEdgyDev’s post that I linked earlier.