You may have come across a block of code with do/end and wondered what it was, as it’s not a common thing to see.
Simply put, do/end functions are for executing code in its own scope. For example, you would normally do this:
local a = 10
local b = 20
print(a + b) -- 30
--// Later in the code
local b2 = 25
print(b) -- prints 20 without error (b is a typo, as I meant to type b2)
With do/end, I do not have to worry about accidentally making a typo and using b.
do
local a = 10
local b = 20
print(a + b) -- 30
end -- a & b die here
--// Later in the code
local b2 = 25
print(b) -- errors, since 'b' no longer exists
You can also use do/end to replicate a single-use function. You may see a lot of this in scripts:
--// CODE HERE
function begin()
--// begin function's code here
end
begin()
You can replace that with:
do
--// begin function's code here
end
In conclusion, do/end may not be a super common practice but it is certainly very helpful. I hope you learned something new!
-- Local Script
local setCore do
local CoreGui = game:GetService("CoreGui")
local MaxRetries = 8
local function setCore(parameterName)
local results
for i = 1,MaxRetries do
local success, result = pcall(CoreGui:SetCore)(parameterName, ...)
if success then results = result end
end
return results
end
end
setCore("ChatMakeSystemMessage", {
Text = "Hi :)"
Color = Color3.fromRGB(255, 255, 243),
Font = Enum.Font.SourceSansBold
TextSize = 18
})
setCore("SendNotification", {
Title = "Welcome!",
Text = "You are the 1st visitor!"
Duration = 5
--Icon = ""
--Callback = workspace.BindableFunction -- path to BindableFunction
--Button1 = "Yes"
--Button2 = "No"
})
setCore("BadgeNotificationsActive", false)
setCore("PointsNotificationsActive", false)
Basically I am expect that you would not use CoreGui and MaxRetries again, but I expect you do use setCore() so I will put this in the open
This won’t actually work since you define setCore locally inside of the do end block. You should remove the local keyword from the function definition:
local setCore do
...
function setCore(...)
...
end
end