Hello, I stumbled upon some code that I’ve never seen before and it looks like this
local varBlock do
-- code
end
And I’m confused on what this means as I know what do-end does but dont know this way of doing it.
thx
Hello, I stumbled upon some code that I’ve never seen before and it looks like this
local varBlock do
-- code
end
And I’m confused on what this means as I know what do-end does but dont know this way of doing it.
thx
I may be mistaken; but, I don’t think that this does anything. This code is equivalent to this code:
local varBlock
do
end
Where varBlock does not actually have any kind of value and do end
will function as it normally does. In Lua you could write all of your code on one line if you wanted to.
example (but do not write your code like this):
local varBlock function double_var(pass1) return pass1 + pass1 end do print(double_var(2)) end -- 4
SummerEquinox is correct. It’s just a style, and a good one at that. All of the code used to define varBlock is inside of that do end block. It’s a way of hiding a lot of mess while also silently telling you what code is used to define the variable and what variable that code affects. If you minimize it, it looks like this.
local varBlock do
You can tell that varBlock exists and is defined, but the code that may have taken up to thirty or so lines and only relevant to varBlock is all hidden.
wait “do end” is a thing?
I’m surprised I never knew this, what can you even do with “do end”?
Hide code and prevent local variables from leaking out. Most people don’t know because they are seldom used. The example in this thread is by far the most common, but most variables can be declared in one line. This is good for variables that look more like this.
local redParts do
local color = BrickColor.new("Bright red")
redParts = {}
for i, v in pairs(workspace:GetChildren()) do
if v:IsA("BasePart") and v.BrickColor == color then
table.insert(redParts, v)
end
end
end
The whole thing is used to create the one variable, and it’s ugly. You’re not going to ever need to glance at this, so with do-end you can hide it and work on the rest of your script without it clogging up your vision. It organizes things.
Edit: better example. The variable color
is only used in the creation of the variable, and because it is local it will not interfere with the rest of the script.