What really "local var1, var2 = value" is? (Confused)

I’m just curious and confused at the same time when I always see this on a pcall() function, So, I want to know what is difference in the two variables?

-- Is this similiar~
local var1, var2 = value

-- ~To this?
local var1 = value
local var2 = value

--Then why is it use it in pcall() like this?
local var1, var2 = pcall(function()
    --Logic
end)

--and getting the argument out of it.
if var1 then
    -- logic
else
    -- logic
end

Sorry, for this silly question, because I’m used to JS or Java alot when assigning like this is like assigning a similar value to a multiple variables using only a shorter declaration like this.

var1 will be used if the pcall was successful.
var2 will be used if the pcall failed.

2 Likes

pcall and some other functions return a tuple. This means it will return multiple values.
local var1, var2 = value should make var2 nil, and var1 value, however local var1, var2 = value1, value2 would make var1 equal to value1 and var2 equal to value2.

1 Like
local var1, var2 = value1, value2

assigns var1 to value and var2 to value2, in languages like JavaScript it would work something like [const/let] var1 = value1, var2 = value2

Functions are allowed to return multiple values in Lua, they aren’t packed into a single tuple datatype or whatever (like Python). cc @YummyLava57

local a, b = (function() return 1, 2 end)()
print(a, b) -- 1 2
4 Likes

A pcall returns two arguments:

  • The first argument, var1, is a bool that returns whether the pcall ran without errors or not.
  • The second argument is the error message if and only if something errored in the pcall. It is useful for printing the error message yourself and fix your code.