Finding out what is calling a certain function

I have a function in a module that is seemingly being called by nothing. I’ve using Find in Place to discover what is calling the function. I found only one source that could potentially be calling this function, putting a print right above the function call, although the print statement inside of what I thought was calling the function does not show up in the console. Is there a way I can figure out where the function in my module is being called?

This is the script I thought was calling the function:

local function initializeMenusButtons(MenusFolder, button)	
	local menuName = button.Name
	local menu = MenusFolder:FindFirstChild(menuName)
	if not menu then return end

	menu.Visible = false

	local closeButton = menu:WaitForChild("Close")
	local originalPosition = menu.Position

	-- Click handling
	local function openMenu()
		UIState.Open(menu, originalPosition)
	end

	local function closeMenu()
		UIState.Close(menu)
	end

	button.MouseButton1Click:Connect(function()
		if (button.Name == "TeamChange" and (TeamChange.canChangeFree or plr.hiddenstats.VIP.Value)) then
			print("Going to call TeamChange.Change() from inside MenuController")	-- does not print
			
			TeamChange.Change(true)
			return
		end

This is the function that is being called:

function TeamChange.Change(CalledByMouseButton1Click)
	print("TeamChange.Change() called")
	
	print("Called by mouse button 1 click function:" .. CalledByMouseButton1Click) -- when run, this prints as "nil" as "CalledByMouseButton1Click" is a "Team" when being passed in
	
	-- determine new color (color of the opposite team) and
	-- corresponding leaderbaord
	local currentTeam = plr.Team and plr.Team.Name
	local movingTo = currentTeam == "Red" and "Blue" or "Red"
	
	task.defer(Loading.makeLoadingScreen, movingTo)
	
	ChangeTeamDisplay.changeHome(movingTo)
	
	TeamSelect:FireServer(movingTo)
end

The second print statement even errors as “CalledByMouseButton1Click” is somehow a “Team” and not a boolean.

In “Find in Place”, these are the only results with search query “.Change(”


Red lines besides in results means should be unrelated.

“CalledByMouseButton1Click” gives this when I hover over:

1 Like

use error() or debug.traceback they should tell you what is calling it.

2 Likes

The above method should work for you.
I also recommend:

  1. opening Window → Debug → Call Stack
  2. Put a breakpoint inside the Change function
  3. Playtest, then checkout the Call Stack window to see the trace
1 Like