What does 'do <code> end' do?

The Below code: How would I make a function which returns all the properties of the given instance? - #4 by CheezeNibletz

local ClassProperties do
	-- ClassProperties["Part"] example
	-- returns table and enable http thing
	ClassProperties = {}
	local HttpService = game:GetService("HttpService")

	local Data = HttpService:JSONDecode(HttpService:GetAsync("https://anaminus.github.io/rbx/json/api/latest.json"))

	for i = 1, #Data do
		local Table = Data[i]
		local Type = Table.type

		if Type == "Class" then
			local ClassData = {}

			local Superclass = ClassProperties[Table.Superclass]

			if Superclass then
				for j = 1, #Superclass do
					ClassData[j] = Superclass[j]
				end
			end

			ClassProperties[Table.Name] = ClassData
		elseif Type == "Property" then
			if not next(Table.tags) then
				local Class = ClassProperties[Table.Class]
				local Property = Table.Name

				local Inserted
				for j = 1, #Class do
					if Property < Class[j] then -- Determine whether `Property` precedes `Class[j]` alphabetically
						Inserted = true
						table.insert(Class, j, Property)
						break
					end
				end

				if not Inserted then
					table.insert(Class, Property)
				end
			end
		elseif Type == "Function" then
		elseif Type == "YieldFunction" then
		elseif Type == "Event" then
		elseif Type == "Callback" then
		elseif Type == "Enum" then
		elseif Type == "EnumItem" then
		end
	end
end

In Moon Animation Script:

-- libraries
do
	for _, lib in pairs(script.Parent.Libraries:GetChildren()) do
		if lib.ClassName == "ModuleScript" then
			global[lib.Name] = require(lib)
		end
	end
end

I can’t know difference between <do ~ end> and only code.

do 
--code 
end

making variables short

local foo do
  local huh = math.pi * 10
  foo = function()
    return huh
  end
end

print(foo())

No, do is a keyword which acts like a new stack (doesn’t create one), used for limiting the scope of variables.

local a = 1

do 
   local a = 2
   print(a) -- 2
end

print(a) -- 1
3 Likes

i don’t know what this means in english, but ok
image

Silent’s answer is correct, it allows you to limit a variables scope to just that block. You can think of it as being equivalent to:

local a = 1

(function ()
  local a = 2
end)()

print(a) -- 1

The difference being, you don’t actually create a function… just a new scope.

local a = 1

do
  local a = 2
end

print(a) -- 1