Fixed thank you!

So, I am making a module script that needs multiple functions…

Is this possible? (im probably just rlly rlly dumb.)

How do you add another function to a module script?

1 Like

Basically, you make a “Dictionary” of functions. For example:

local module = {}

module.print1 = function()
    print(1)
end
module.print2 = function()
    print(2)
end

return module

How to use the functions:

local m = require(pathToModuleHere)
m.print1()
m.print2()
--Prints:
--1
--2
2 Likes

That depends on what you mean by functions. If you mean functions at all, then you can just throw them into the ModuleScript as normal - you will only be able to use those within the ModuleScript itself though. If you want your ModuleScript to return more than one function, then like the above mentioned, you’ll want to include your functions in a dictionary and return them.

local module = {}

local function privateFunction(parameters)
    something(parameters)
end

function module1.publicFunction(parameters)
    something(paramaeters)
end

return module

You can also return a dictionary after creating it rather than having it assigned to a variable at the top of your script.

local function publicFunction(parameters)
    something(parameters)
end

return {
    method1 = publicFunction
}

On an unrelated note, you can also return things from returned functions.

-- Module
local module = {}

function module1.GetAB()
    return "AB"
end

return module

-- Script
local module = require(module)
local abString = module.GetAB()

print(abString) --> AB
2 Likes

Nothing has worked so far. Here is a pic of the module script.

How are you using this module? Are you calling the functions? I need more info.

local module = require(pathToIt)
module.credit()
--Prints credits

If you wanted to have code run when the module is required, just put it in the normal script, or call the function.

To solve this issue, the function requires a self argument. It should end up like so:

module.pjoin = function(self,player)
    print(player.Name)
end

I prefer it like this however:

function module:pjoin(player)
   print(player.Name)
end

EDIT: Basically, provide the self (or any variable you don’t need I guess) argument to the “.” syntax function if you’re passing arguments through the function for use.

image

Firstly, where do you define module?

Secondly, this will only run if the module has replicated, which may be delayed. (Try :WaitForChild)

Thirdly, please put your code in code blocks (I can not modify your code without manually typing it out again, which is a pain.
Typing: (Without the >)

--Code here

Transforms to:

--Code here
local module = require(game.Workspace.MainModule)

game.Players.PlayerAdded:Connect(function(player) 

	if game.Workspace:FindFirstChild("MainModule") then
		module.pjoin(player)
		module.credit()
     end
end)
local module = {
	
	["update"] = true,
	
	["test"] = false,
	
}

module.credit = function()
	print([[
	"Thank you for installing a product from Coastal Customs."
	"For help or support dm.."
	"coast#4552"
	"CREDIT: Coastal Customs"]])
end

module.pjoin = function(player)
	print(player.Name)
end

return module

What shows up in the console? Does anything print? Have you tried adding a :WaitForChild?

1 Like

Modules can only be used if they’re required.

-- ModuleScript
local module = {
    ["update"] = true,
    ["test"] = false,
}

module.credit = function()
    -- thing
end

module.pjoin = function()
    -- thing
end

return module

-- Your actual script
local module = require(game.ServerScriptService.Module) -- Example
module.credit()

Another note - hopefully, you have these two scripts separate. You need to put the actual ModuleScript code in a ModuleScript, not the same script.

1 Like

It all just printed correctly, oh my I’m so dumb. Thank you guys for your help. I didn’t require it right ofc.

Just because you did not realize something does not make you dumb. The purpose of this category is to help developers out, not make them feel down. Everyone makes mistakes. The longer you practice coding, the less you will make.

With that, Have a good day. You are not dumb.

2 Likes

Any difference in functionality as making functions like this:

function module:credit -- etc end

or is it just syntactic sugar?

Yes there is. Colon syntax passes the table you called the function from as an implicit first argument, self. This pattern is commonly used when writing pseudoclasses in Lua.

local module = {
    privateAB = "CD"
}

function module:method(other)
    print(self.privateAB..other)
end

function module.method2(self, other) -- Same as the above
    print(self.privateAB..other)
end

return module

module is passed first, then other. It’d be the same as calling module.method(module, other). This pattern also works with Roblox objects. Notice how you usually call methods with colon syntax? It’s passing itself as an argument. For example, the two calls below are the same.

local DataStoreService = game:GetService("DataStore")

local dataStore = DataStoreService:GetDataStore("ABTest")
local dataStore = DataStoreService.GetDataStore(DataStoreService, "ABTest")
1 Like