Can someone explain the following code:
local coreCall do local MAX_RETRIES = 8 local StarterGui = game:GetService('StarterGui') local RunService = game:GetService('RunService') function coreCall(method, ...) local result = {} for retries = 1, MAX_RETRIES do result = {pcall(StarterGui[method], StarterGui, ...)} if result[1] then break end RunService.Stepped:Wait() end return unpack(result) end end coreCall('SetCore', 'ResetButtonCallback', false)
Questions I had in particular were:
1) What does local coreCall do
mean? Is it simply another way to define a function? Is there a special usecase?
2) Is there a reason why MAX_RETRIES is only set to 8? Wouldn't this mean if it retried 8 times then the desired effect would not occur? Wouldn't it be better to not have a MAX_RETRIES?
Thank you!
That is almost useless do statement, you should not be using it, however you can't hate it as it creates static variables for optimization which is cool. do statements in Lua create new scope preventing variables to be accessed from higher scopes, like this
local A = true do local B = true end print(A, B) -- true, nil
In your case he just put do after the variable making it bit more confusing, but he did same thing as this
local coreCall = nil local coreCall --\\those 2 are almost identical, however, use the second method. do --/ Only defines this variable once local MAX_TRIES = 8 ... --/ Not local function, must initialize the coreCall --\ variable in the outer scope so you can access --\ it outside of the do statement function coreCall(...) repeat ... for MAX_TRIES ... end end print(MAX_TRIES) -- nil coreCall(...) -- works
Here you can see that the variable MAX_TRIES is defined once in the do statement and then it will be used by the coreCall function, you can do
local function coreCall(...) local MAX_TRIES = 8 repeat ... for MAX_TRIES ... end
or
local MAX_TRIES = 8 local function coreCall(...) repeat ... for MAX_TRIES ... end
However, the first method would create MAX_TRIES every time you call the function and the second method would flood the scope with unneccessary variable, like you are not going to use MAX_TRIES outside of the coreCall function, this is why it is written with do statement.
The second one is simple, say Roblox disables CoreGui named ResetButtonCallback, if you would not have MAX_TRIES, it will loop infiintely trying to set the core but it will never be able to as Roblox has disabled the method, that's why MAX_TRIES is made, it will loop for 8 times and if it won't work then it's mostly because the CoreGui thing is not enabled.