Issue with getting function name!

I’m currently making a admin command gui/script, and I’m confused on how I would get a function name.
image
In this photo you can see that I have a function with a string name, and I wanted to get this string, so I used

print(cmdFunctions.testprint())

This however, prints… nothing. (it also runs the function, not what Im looking for)
image

One last thing, I have already tried to run

print(cmdFunctions.testprint)

And it just returned the function memory address (I believe thats what it is showing, but it doesnt really matter)
image

If you have a solution please comment it!

Trying it now, I’ll let you know if it works

Same exact result, It prints nothing.

Actually, I misunderstood, it prints the function address because you are trying print the function not “test(” you are trying to print.

You could make “testprint” in the table equal “test(”
or just run the function without the print brackets at the bottom

Because you’re printing a function, you’re calling the function inside another function, making the script thinks you’re trying to print out a function.

Just do cmdFunctions["testprint"]().

1 Like

The example you provided prints nothing because your function returns nothing. Try this instead:

local cmdFunctions = {
	["testprint"] = function()
		print("test(")
		return "Test2"
	end,
}

print(cmdFunctions.testprint())

If that still doesn’t work then please show your full script because something else is going on.

Okay, I’ll do something similar. I’ll have something like this

local cmdFunctions = {
   testprint = function(testing)
          if testing == false then
                 -- code
          else
          return "testprint"
          end
   end)
}

Also, I’m not exactly sure if this is what you meant to make me do, but It works for me either way.

You technically already have the function “name”. When you do cmdFunctions.testprint(), “testprint” is the name

local name = "testprint"
local func = cmdFunctions[name]

print("Function name:", name)
func()

You can use a for loop and iterate the cmdFunctions dictionary which would give you every key (func name) and value (func) (if you’re trying to display all commands for example)

local cmdFunctions = {
	testprint = function()
		print("testprint function called")
	end,
}

for name, func in pairs(cmdFunctions) do
	print("Function name:", name) -- > Function name: testprint
	func() -- > testprint function called
end