Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Very quick Modulescript question - help?

Can a module script in any way use a recursive function?

for example

function module.EditAllParts(Parts, Transparency, Color, Visible)
...
	EditAllParts(v, Transparency)
...
end

Will create an error message, "Unknown global EditAllParts" Any way around this?

2 answers

✓ Accepted answer

Yes, and I used this to create a GetDescendants module.

Think of it this way: Every variable that you create in a script is stored in a table called the environment. The environment in your script:

{
	module = {
		function EditAllParts(arguments)
			--lots of stuff
		end
	}
}

So, you can't just access the function directly, as the function isn't a direct member of the environment. Much like you can't access a part like game.Part. You have to access the module first, then call EditAllParts from there.

function module.EditAllParts(Parts, Transparency, Color, Visible)
	--stuff
	module.EditAllParts(v, Transparency) --Remember the module
	--more stuff
end

Another option is to define your function normally:

function myWhatever(n)
	myWhatever(n - 1)
end

and then just export that function:

local module = {
	myWhatever = myWhatever
}
return module
-- or, simply `return myWhatever`
---------
local r = require(modulescript)
r.myWhatever(10)

This discussion is preserved as it was. New answers, comments, and votes are closed.