Is this possible with module scripts?

I want to get data from a module script to use in another module script, i want this to depend on a string value in the player
Screenshot 2025-04-25 191125
i want it to get the children from this folder, inside the module script theres data in the ms that i want to use in another module script, it looks something like this:
Screenshot 2025-04-25 191318
say i want the speedmult data from this, how can i put that value into another ms? I tried this with remote events but they only apply for local scripts

Hi there. You could use OOP (Object Oriented Programming).
For example, in the local script:

local DataModule = require(module.path) -- the module that the data is stored in.
local DestinationModule = require(module.path) -- the module that you want that data to be put into
Local Value = LocalPlayer.Value 

if Value.Value == true then
 DestinationModule.StoreValues(DataModule.Data --(for example the swordie name...)) -- this calls a function inside the DestinationModule that will get the values by its parameters. So for example if you would want to pass the name, you will write "swordie.name"
end

then on the DestinationModule, you would create the function to store these values, that we called on the localscript; for example:

local module = ()

function module.StoreValues(parameter1,parameter2) -- the parameters are the stuff that will be passed by the local script, from the DataModule
-- Now you got your values. you can store them, use them or whatever :)
end

end

Let me know if this helps :slight_smile:

You should just be able to require the module and reference swordie.name also if your module script is just holding data you can do this, same thing but more pretty to look at

-- Swordie Module Script
return {
  Name = "swordie",
  Health = 90,
  SpeedMultiplier = 1,
}

-- Where ever you want to reference it (Another Module)
local swordie = require(path.To.Swordie)
print(swordie) -- Should return a table of name, health, speedmultiplier.
print(swordie.Name)

Also since its data my preference is to make everything Capitalize as its a strong point. It’s up to you and what you feel comfortable with!

oops yeah , didnt think about it :sweat_smile: This solution is better.

Yours is still good if @TafareWoomy ends up letting swordie have functions through OOP or Util functions. if its just holding data my way might be better but if it has functions your way is the way to go :sunglasses:

1 Like

but what if i want multiple module scripts to require the data from that single module script?

I’m not understanding what you mean? You can just make multiple requires for where ever you need it?

-- Swordie Module Script
return {
  Name = "swordie",
  Health = 90,
  SpeedMultiplier = 1,
}

-- Module A
local swordie = require(path.To.Swordie)
print(swordie) -- Should return a table of name, health, speedmultiplier.
print(swordie.Name)

-- Module B
local swordie = require(path.To.Swordie)
print(swordie) -- Should return a table of name, health, speedmultiplier.
print(swordie.Health)

I’m assuming you want to just get the value that is stored from one module script, and put it into a different modulescript. Here’s how you would do it:

local swordie = {}

-- Misc

swordie.name = "swordie"
swordie.health = 90
swordie.speedMult = 1
return swordie
-- Some other modulescript
local module = {}
local otherModuleScript = require(swordieModuleScript) -- you might need to use :WaitForChild() to get this modulescript
module.valueFromOtherModuleScript = otherModuleScript.speedMult -- or some other value that you might want
return module

You don’t need to change anything in the example that I provided you with for this to work with multiple modulescripts.

1 Like

Wouldn’t it make the script look a bit unorganised if theres alot of require paths in the script with the data for each and every single module script that needs it? Shouldn’t there be a few lines of code that allows each script that needs the data to have it

if you want to one line it you can. I suggest not doing this as it just looks ugly.

Edit: whoops this is what @x86_architecture said i didn’t see that :melting_face:

local swordieHealth = require(path.to.swordie).Health

If you’re getting the data from other modules yeah you’ll need to require it each time that’s just how modules work, if it’s within the same script you can just require it at the top and reuse it throughout the script.

Just make sure you don’t change the values by accident cause it will update to those other scripts that have it required.

1 Like

It would probably be something like this

But yeah this also works too if i wanted to use it

You should be able to change the values regardless unless you have a strong reference to the value

local StrongReference= require(path.to.swordie).Health
versus
local SoftReference= require(path.to.swordie)

SoftReference variable should be able to access the module since its a soft reference and not a hard reference like the Health

I’m not sure, I’ll have to double check that but I know in one of my games I had an issue where when a player spawned a car if they owned it then it added their data to that car and returned all the information back to the client but It had a bug I didn’t realize where it was updating the actual car data and returning that so if someone else spawned a car it started mixing the two players data together.

that’s why i recommend if you plan on changing the data make sure to clone the table so you don’t accidentally do same mistake.

Yeah for that you should of stored a table for that has each player, if you don’t you get what you experienced where anyone can mess with the module data. Instead you can swap it out for a Template Data that every vehicle gets when spawned then store it to another table called like plyVehicleTable which holds all the player vehicles and when they go to spawn it, you find the target vehicle and load all the data. This way it won’t overwrite any other data for vehicles. I hope that makes sense to you?

1 Like