How could I pass data to a module script

I have been learning about module scripts and from what I understand a module script is a way to store data inside of them such as functions or info and then load it into a script using require, So my issue here is I would want my module script to play animation tracks, check damage values sent to it and others but since you cant send an argument by requring it how could I pass data to it?

3 Likes

A module script can be alot of things, here’s what i’d do for assigning data inside of a module script.

-- // Module Script
local MainModule = { }

function MainModule:GetData()
    -- You could reference `self` however I wont for simplicity reasons.
    return MainModule.Data
end

return MainModule
-- // Script/LocalScript
local ModuleScript = require(script.MainModule)

ModuleScript.Data = { DataObject1 = {  Name = "Object" } }

print(ModuleScript:GetData()) 
1 Like

The simplest way to do this is expose functions within your module to the script that requires it. Then you can use the module’s functionality directly.

-- Module
Module = {}
Module.Print = function(Argument)
	print(Argument)
end
return Module
-- Script
Module = require(script.Module)
Module.Print("Hello World!")
3 Likes

Wait so I can pass arguments to the functions inside the module script, But not the module script directly?

Again Im not trying to make a module return me something, What im trying to do is find a way to send a table or soomething to it so it can use it in the functions I want it to use, Or try and see if the module can grab data from anotther script to then use it in the funcction its running.

-- // Script 1
local Module = require(Module)

Module.AbcValue = 123
-- // Script 2
local Module = require(Module)
local Value = Module.AbcValue

print(Value)