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
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
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.
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
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.
>;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
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
})
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