Random question

why sometimes pepole use "_"as a parameter in functions?

1 Like

The use of an underscore as an identifier doesn’t really mean anything special in Lua, or in most languages for that matter. Programmers use it to represent nothing. Say, for instance, you had a function that returns 2 values, but in your situation the first one doesn’t matter. You can’t just skip it, you still need to account for it because the order of how data is returned is based on position. For instance:

local function get_name()
    return "John", "Doe"
end

local _, last_name = get_name()

Because defining first_name would really be unnecessary, an underscore is used and that is how programmers say they won’t be using that variable.

However an underscore is still a valid identifier so you can still use it.

print(_) -- John

But that kind of ruins the whole purpose.

(I just copy-pasted my response from May someone explain to me what for _, in pairs means? - #5 by sjr04 lol)

4 Likes

It is generally use to just disregard the value, and you will tend to see this or use this in script connections, or some functions that returns multiple values but you only want some of them as stated earlier, Example in a script connection:

--say for example we hook up a function to the ancestry changed event
somePart.AncestryChanged:Connect(function(_,Parent)
--In this example we are disregarding the child param in the script connection
end)

I think it is for when you have two variables and you want only the second one. So to spare time and to kind of skip the first variable people use _ or any other Sign.
Hope you found that useful

1 Like

salve mano, manja dos ingles :wink: