Heya, thanks for reading in advance. I’d like to know if there’s a way to call local functions using a instance’s string instead of something like “Commonalities[Instance.Name]” in a module. I tend to have an optimization habit and I know it’s better to use local functions whenever you can. Doesn’t really matter all that much, just some curiosity! Insight is appreciated either way.
(In this case I get the instance from a simple iteration)
You can use getfenv() to access variables inside of the script.
function testFunction()
print("hello")
end
local variables = getfenv()
variables["testFunction"]() -- prints "hello"
Do note though, that getfenv() and the related setfenv() are deprecated functions. Also, you won’t be able to use native code generation by using the function.
I don’t know if you’re referencing a specific principle, but I have personally never heard this advice. Use modules if it means you won’t have to rewrite the same code across multiple scripts
I see, alright, it’s just that I’ve heard of them being slightly faster, guess I’ll stick to the same way I’m doing it.
https://create.roblox.com/docs/luau/scope
“Variables and functions have global scope by default, but it’s almost always better to declare them with local scope because Luau accesses local variables and functions faster than global ones”
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.