I usually se it in scripts written like:
for _, in pairs (something)
I don't understand what they are, if you could explain them to me, that'll be perfect!
The underscore character alone doesn't have any significant meaning in Lua, or most languages for that matter. It simply just exists to represent nothing, or a blank space. Say for example you have a function that returns 2 values, but you don't care about the first one. You can't just skip over it, you still need to define it since the order of how data is returned depends on position. Here's an example:
local function test() return "first", "second" end -- We need to create two variables, even though we only care about getting "second". It's not bad that we defined "first" even though we're not going to use it, thought it's still unnecessary. local first, second = test()
Because of how unnecessary defining "first" was in that example, most people just replace variables they're not going to use, with an underscore. Like this:
local function test() return "first", "second" end -- Now we just replaced "first" with "_" to indicate we won't be using it. Don't let this confuse you, however. The underscore is still treated as a normal character, and it still holds the value it corresponds to. local _, second = test() -- To prove this, if I passed the underscore variable in a print function like this: print(_) -- It would still print "first".
The same thing goes for your for loop question:
-- Indicates we don't care about "i", the index, even though we could still use it by using "_". It's just a visual preference. for _, v in pairs({}) do -- Code end
Hope this helped.
It's refered to as 'pairs' in roblox and it is also used to access the values of a table sometimes and if you want to you can make it hold many different variables. The pairs usually work like this:
for i,v in pairs(game.Workspace:GetChildren()) do v. --Whatever you want to do with the children of workspace.... end
That's 1 way to use it but, it plays a huge role in admin scripts as well.
P.S I didn't mention anything about the pairs with that underscore because it's been explained above I just wanted to explain what pairs can be used for and tried to help you understand what they are. Sorry I'm a bit new to this.
I believe _ is simply a rarely used variable, so people decide to use it in studio. _ can also define _G (global) variables, such as _G.variable=4
Hope this helped
~Chem
Locked by User#19524
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?