This somewhat sounds like a stupid question considering that we use functions inside of modules all the time. Let’s say I have a combat that is object oriented and I use .new() for every combo would there be no risks or should I stay cautious?
when you say combat do you mean your calling the new function every time the player attacks?
There is very little difference (from my test, you’re only saving at the very most 0.000014 seconds per 10,000 calls by saving the function to a variable)
local ct = os.clock()
for i = 1,10000 do
Vector3.new()
end
print('With indexing',os.clock() - ct)
task.wait(1)
local v3 = Vector3.new
local ct = os.clock()
for i = 1,10000 do
v3()
end
print('Without indexing',os.clock() - ct)
If you’re really concerned, you can just index the table once and save that function to a variable but that’s going to impact the organizational factor of your scripts. IMO you gain much more by not saving the function to a variable than you gain by saving to a variable but I suppose it’s up to you. But realistically, if this was negatively impacting devs, Roblox probably would have made all of the Roblox lua globals functions instead of tables with constructors. Also, using t.new() as a constructor extends much further than just Roblox with respect to Lua OOP, and there is not a single post that advises against it.
To put this into perspective, you really only start seeing a visible difference when calling your constructor a billion times, and even then, the difference is only .25 seconds so I mean…
It’s also unlikely that you’ll be working with more than hundreds to even thousands of objects at the very most.