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

Requiring and using a modulescript inside of a modulescript?

Asked by 10 years ago

I am using simplified code, but I am having a problem.

I have code that is somewhat similar to this on a fundamental basis:

(Inside of a module script named abcs)

function ABC()

local a = require(workspace.yes)

print(a.b())


return{}--What to return?

end

A, is a modulescript with the function b inside of it.

B:

function b()

return nil

end

Now when I try to require ABC I get this:

02:37:49.662 - Requested module experienced an error while loading 02:37:49.662 - Script 'wsk = require(workspace.abcs)', Line 1 02:37:49.662 - Stack End

Someone on the ROBLOX forum told me that all functions used in a modulescript must be returned, but I don't know how to return it and am having a lot of trouble.

Can you just not use modulescripts inside of modulescripts?

1 answer

Log in to vote
4
Answered by
Merely 2122 Moderation Voter Community Moderator
10 years ago

You can definitely use ModuleScripts inside of ModuleScripts. I'm not sure what you're trying to achieve with your code, but it looks like you're confused about how ModuleScripts work.

ModuleScripts can return two things: a function, or a table.

ModuleScript (in ReplicatedStorage)

local double = function(x)
    return x * 2
end

return double

Usage:

local double = require(game.ReplicatedStorage.ModuleScript)
print(double(2)) --> 4

Or it can return a table (in Lua, a table is basically a list).

TimeModule

local Time = {}

function Time.seconds(seconds)
    return math.ceil( seconds  )
end

function Time.minutes(minutes)
    return math.ceil( minutes * secondsPerMinute )
end

function Time.hours(hours)
    return math.ceil( hours * secondsPerHour)
end

function Time.days(days)
    return math.ceil( days * secondsPerDay  )
end

...

return Time

Usage:

local Time = require(game.ReplicatedStorage.TimeModule)
local week = Time.days(7)
0
I kind of understand now. Thank you for the answer, it has helped me a lot. DonFeskeral 0 — 10y
Ad

Answer this question