Need help understanding reset button script

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, ...)} -- Need explaination on this part
			if result[1] then
				break
			end
			RunService.Stepped:Wait()
		end
		return unpack(result)
	end
end

assert(coreCall('SetCore', 'ResetButtonCallback', false)) -- Confused why we use assert here, there isnt a second parameter which is supposed to be a error message 

-- also why do we even need the unpack(result) stuff

result = {pcall(StarterGui[method], StarterGui, ...)} -- don't understand
-- is it just a different version of this?
StarterGui:SetCore("ResetButtonCallback", resetBindable)

from here

If I had to assume, it seems to be a way to attempt to disable the ResetButtonCallback callback to disable resetting all together. Looking carefully at the code, it seems to be doing the following

  • Set up variables for the maximum amount of retries to disable the callback and variables for StarterGui and RunService
  • Create a function called coreCall which accepts the first parameter into the method parameter and any number of parameters into the ... parameter/variadic function
  • Create a variable containing an empty table and for how many times we should retry:
  • Set result to contain the pcall result of attempting to disable the ResetbuttonCallback, and if the first thing in the table is true, stop the loop, otherwise, wait a frame and try again
  • return the unpacked contents of result
  • Run the coreCall function and if there was an error during the function, it’ll print out the error given in red, otherwise nothing will happen besides ResetButtonCallBack being false now, meaning Resetting is disabled

I’ll explain your 2 questions:

assert is used in this case because of how pcall works, when you pcall something, it means that you are error handling, as a pcall generally returns two things, the first one being either true if the pcall finished with no errors, or false if an error was found. If true was given, the 2nd thing given gives you the returns of the code in there or the error message if it was false

An example of a pcall would be this

local success, value = pcall(function()
	return "This will work"
end)

print(success, value) --prints true This will work

success, value = pcall(function()
	print("Hello"..false) 
	return "Hi!"
end)

print(success,value) --prints false Attempt to concatenate string with boolean

unpack is a function that returns the elements/contents of a table, it expects 3 things, but the first one is mostly used as the other 2 are optional. You need to give it a table to unpack, here’s an example of unpack

local tbl = {1,2,3}

local one,two,three = unpack(tbl)

print(tbl) --Prints {1,2,3}
print(one,two,three) --Prints 1 2 3

This works because the first 3 returns from unpacking the ta ble are stored in those variables.

unpack is needed in that function since once again pcall returns 2 things and if you don’t unpack it probably would error when it tries to set the first thing of assert to a table.

assert will do 2 things if the code works or if it error:

Lets say the code works as expected, in the end, the assert will see

assert(true, "")

SetCore probably doesn’t return anything so the 2nd thing will probably be empty, because the first argument is true, nothing will show up since the first argument must be false for it to display thr 2nd arguemnt as an error

Otherwise, if an error was found, assert will see this

assert(false, ErrorMessage)

Where ErrorMessage is the text of the error that was given. Here the first argument is false, so it will display the contents of ErrorMessage as an error

Those do 2 different things, the first one is attempting to disable resetting, the 2nd is creating custom reset logic

2 Likes