So recently i’ve seen do functions been used like this
local variable do
variable = blah blah blah
end
But what does it do? Whats the point? Is it stylistic? Are there any advantages?
So recently i’ve seen do functions been used like this
local variable do
variable = blah blah blah
end
But what does it do? Whats the point? Is it stylistic? Are there any advantages?
What does do do?
As far as i know, it is just used to create another scope. So like if you declare a local variable inside your example, it will not be accessible outside of that do - end
scooby do be do
So are there any advantages to this? Or does it just make your code look cleaner.
scooby do be do where are you
You can use this when you assigning a value to a variable is a complicated process, for example
local variable do
--do a bunch of stuff, make variables and functions, and at the end do
variable = --whatever
end
so you just did 100 lines of code to give a value to a variable, but to your script it looks as simple as
variable = --something
so basically yeah, mostly used just for a cleaner code
so basically its just cleaner code
so basically yeah. ( i am adding random text, because roblox requires more text for reply)
as far as I am aware it is useful in helping some things get collected faster
do
local james = workspace.James:GetChildren()
print(#james)
for i, v in pairs(james) do
print (v. Name)
end
end
Hopefully that helps explain. It’s not just cleaner. It’s not like adding 2 words makes the code any cleaner. But I think it does make the garbage collector slightly more efficient. for references within the do.
Yes, here are some basic examples :
local myVar do
print("Hi there") -- Hi there
myVar = 5
print(myVar) -- 5
end
local myTab ={} do
print("Hello World") -- Hello World
for _,child in pairs(game.Workspace:GetChildren()) do
table.insert(myTab,child.Name)
print(child) -- child name
end
end
do
local stuff = game.Workspace["HD Admin"]:GetChildren()
print(#stuff,stuff[1])
end
local Bool do
Bool = true
if Bool then
print(true)
end
end
extremely slightly. That wouldnt affect performance anyhow. And it makes it faster because it declares things inside a scope, and outside of that scope they are not accessible.
The same logic here applies, why you should always use local with a variable, if its is not needed outside of a scope. It wont make 20 fps run 60, but it is still best practice
I’m assuming you’ve seen that convention in the context of event connections, i.e;
local remoteEvent = workspace.RemoteEvent
local connection do
connection = remoteEvent.OnServerEvent:Connect(function(player)
print(player.Name)
connection:Disconnect()
end)
end
This is essentially a syntactic sugar for the following.
local remoteEvent = workspace.RemoteEvent
local connection
connection = remoteEvent.OnServerEvent:Connect(function(player)
print(player.Name)
connection:Disconnect()
end)
This is done so that the reference to the variable named “connection” from within the lambda function’s body is within scope, at that point the variable has been declared, on the other hand if you were to do the following.
local remoteEvent = workspace.RemoteEvent
local connection = remoteEvent.OnServerEvent:Connect(function(player)
print(player.Name)
connection:Disconnect()
end)
The reference to the variable named “connection” from within the lambda function’s body would not be in scope as at that point the variable has not been declared, this code would result in an error similar to the following, attempt to index nil with "Disconnect"
.
do end
blocks are required for the iteration structures (for and while) their use in that context isn’t related to garbage collection. That’s just how Lua’s syntax operates.