Any way to get function's name?

So let’s say I have a function like

function doStuff()
	print"ok"
end

Is there a way to reference the name of the function (doStuff) as a string when referring to the function without having to write the function in a special way?

If you do something like

print(tostring(doStuff))

It’ll just print something wierd like
function: 0x12e3c21afacee547

EDIT:
For those curious as to what this is for, I wrote a module for Easy Client to Server communication

The module requires you to state the name of the function separately.
It would be cool if I could just go

giveClientAccess = require(module)

function doStuff()
 print"ok"
end

giveClientAccess(doStuff)

Is there some weird wacky metatable-ish way to do this?

4 Likes

You should explain what you need this for so we can provide a more precise solution.
I’d propose you place the function as either a key or a value in a dictionary so you can compare the values to what you need

1 Like

You can’t really get the name, all you printed was the identifier of the function. Any purpose of printing the function’s name?

You could do the names like this instead:

local funcTable = {
    ["NameOfFunction"] = function()
        -- woah it's a function
    end
}
3 Likes

Alright, I’ve edited the OP to make things more clear.

You could probably do something with getFenv(), loop through all indexes to find what matches the signature of the function. Very hack though and probably not worth it.

1 Like
function getName(funct)
	for i,v in pairs(getfenv()) do
		if v == funct then
			return i
		end
	end
end

That works perfectly!
I don’t think this will effect performance all that much either.
Is there anything truly wrong with doing something like this?

1 Like

I spoke too soon.
It looks like you can’t really pass envs from a script to a module ;c
sad

I’ve been trying to find a workaround to no avail.

The only issue might be the expanding amount of code you add up into the script, causing the for loop to run through a lot of things.

Try using getfenv(0), however it’s not that epic if there are more variables and functions in the global scope.

1 Like

is getfenv(0) different from just getfenv()?

OH EPIC!
this works

Ok so I have done some testing.

I wrote this piece of code to write code for me:

for i=0,1000 do
print([[function a]]..i..[[()
	print('hi]]..i..[[')
end]])
end

Then I pasted that code inside the script so that there are 1,000 unique global functions.
Roblox’s server didn’t even flinch (even when I had it print the function names).
This works perfectly.

for all intents and purposes, I hereby pronounce this

still epic

Don’t do this. Use of getfenv/setfenv adversely affects script performance with Luau and is incompatible with the upcoming Typed Lua feature. From a Roblox engineer:

Using getfenv/setfenv will penalize access to builtin globals (math, game, etc.) in any threads that were ran from the same script, and (with a subsequent set of changes that hasn’t happened yet) will disable optimizations on some built-in functions like math.max etc.

Also worth noting is that getfenv/setfenv will not be compatible with the Typed Lua - in typed scripts, using getfenv/setfenv will violate type invariants so code that seems type-safe won’t be. And I guess script analyzer has been warning about this for a while now. The current preview doesn’t have type support yet so that’s coming later this year, but worth keeping in mind - getfenv/setfenv is just not a sound mechanism going forward.

Store the function in a regular dictionary instead, with keys as names and values as the function.

2 Likes

fRiCk!

>;C
i revoke my statement about this still being epic and stuff

Is there any way to get this same functionality?
I’ve built in a thing in my module just incase this became too resource-intensive anyway so it’s not a huge deal but I’d really like to keep it how it is

1 Like

Store functions in a regular dictionary with keys as function names and values as functions.

1 Like

This is as previously stated in an earlier reply in this thread. I have always done this method instead of using getfenv(0).

1 Like

I guess I’m just incapable of reading.
I’m staying up really late so lack of sleep might have something to do with that.

This is how I think I’ll format my module for v4:

GiveClientAccess = require(game.ServerStorage.ServerSide)

GiveClientAccess({
	test = function(Player,Brick_Color)
		
		Brick_Color = Brick_Color or BrickColor.Black()
		
		print(Player.Name..' wants to spam '..tostring(Brick_Color)..' parts!')
		
		spawn(function()
			while wait(.25) do
				Instance.new('Part',workspace).BrickColor = Brick_Color
			end
		end)
		
		return true
	
	end
})

Thanks for your help!

https://developer.roblox.com/en-us/resources/release-note/Release-Notes-for-407

This is confusing so is it fixed or I am missing something important?

Not only that but how much impact does it have on performance overall?

2 Likes

I’m 99% sure that that is in relation to Setting properties using the environment table from getfenv(0) does not work until certain expressions are used (in Luau). Not what Dandystan is talking abuot.

2 Likes

I see thanks for the clarification, but more importantly how much impact does it have on performance because if it’s negligible I don’t think it’s worth being a concern.

Also worth noting is that getfenv/setfenv will not be compatible with the Typed Lua

not sure what that is either but I probably won’t be using it so even if I use getfenv/setfenv I’m not affected.

Typed Lua is a planned feature that will allow you to essentially lock the parameter of a function to a specific type. Errors will be thrown for passing a value of an unaccepted type. It’s equivalent to writing out a list of asserts at the top of a function.

local BAD_ARGUMENT = "bad argument #%d (%s expected, got %s)"

local function expectAString(parameter)
    assert(parameter, BAD_ARGUMENT:format(1, "string", "no value"))
    assert(typeof(parameter) == "string", BAD_ARGUMENT:format(1, "string", typeof(parameter)))

    print(parameter)
end
1 Like