Question about modules and functions

Hello!

I have a question about this module “configuration”.

local module = {
	['DamageBricks'] = (function()
		local bricks = {}
		for i,v in pairs(workspace:GetDescendants()) do
			if v:IsA('BasePart') then
				if v:GetAttribute('DamagePart') and v:GetAttribute('DamageToTake') then
					bricks[#bricks + 1] = v
				end
			end
		end
		return bricks
	end)()
}

return module

Here’s what I have.

Now, does the function run when I first require it, every single time I require it or only when the game starts?

If it’s the second option, is it any more efficient than requiring it and then calling the function itself?

Quick research shows it cannot be the 2nd option because subsequence calls will be the same table

For the other 2, since module scripts only run when they are required, I would have to assume your function would only run the first time y ou require it, any other calls to require it will not rerun agian since it would return the same table, assume you only call it from one side, meaning it can’t be the 3rd thing mentioned.

So in the end, from what I found at least, it’s going to only run once when you require it

1 Like