Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Very quick Modulescript question - help?

Asked by 8 years ago

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

Log in to vote
1
Answered by
funyun 958 Moderation Voter
8 years ago

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
Ad
Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

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)

Answer this question