How do you send a variable from a server script to a module

It’s like

local module = {}
return module

But in reverse since i need to send a variable from the server to a module.

This post might help:

1 Like

So basically just

local mod = require(module here)
mod.Variable = "airpodshotty"
local mod = {}
Print(mod.Variable)
1 Like

yup!
Script using module:

local module = require(yupModuleReferenceInHere)
task.spawn(function()
   while true do
      print(module.Variable)
      wait(0.25)
   end
end)

wait(10)
warn("changing module variable")
module.Variable = false

Module:

local module = {}
module.Variable = true
return module

“Cannot add property (name here) to “module””
and a for loop in the module:
key “Map” not found in module

Sorry idk what you doing in there. I was just showing that the script can change a variable in a module, its a very basic example, where you are getting all that stuff?
BasicCheck.rbxl (37.6 KB)

sorry if this is absolutely disgusting since i just started trying moduled

local module = {}
for _, buttons in pairs(module.Map:GetDescendants()) do
	
end
return module

script:

local module = require(workspace.ButtonModule)
module.Map = workspace.Baseplate

Well, think on a module as a system, that can hold many functions, and many variables that can be accessed and called from any other external script that has a line requiring the module.

So any script can require(Module), so that script can call functions inside the module and change variables inside that module without using BindableEvents.

You want to run a loop in the module, and you want to set a variable in the module, and you want to do it from an external script, then

The module, its a good practice to have your modules stored in ServerScriptService if those are owned by ServerSide

local module = {}

module.Map = false

module.RunAFunction = function()
	for _, button in pairs(module.Map:GetDescendants()) do
		-- Cheking each descendant inside module.Map variable which references Workspace.Baseplate
		warn("descendant found:", button)
	end	
end

return module

The script that required the module to use its functions and variables:

local module = require(game.ServerScriptService:WaitForChild("ModuleScript")) -- Where the module is

wait(5) -- Wait 5 seconds before run the script

-- Set module.Map variable to Baseplate thing
warn("Setting module.Map variable")
module.Map = workspace.Baseplate

wait(3)
-- Running a function inside the module
warn("running a function inside the module to do a for loop")
module.RunAFunction()

Im just giving examples, theres many different ways you could use a Module, Im just pointing very basic details that could be useful to understand how a Module works, Im not saying thats the best approach or understanding whats your goal

1 Like

Maybe you can use the global variable _G

1 Like

I think that firstly understanding what a module does is more important than just jumping into a more complex stuff without knowing how a module works xD :sweat_smile:

_G is a gloval variable. There’s nothing complex about that

I meant for understanding the Module… OP wants to understand how to use a module

Might’ve missunserstood then ¯_(ツ)_/¯

1 Like

yeah, we dont even know what OP wants, Im just trying to help to understand the basics of a module ¯_(ツ)_/¯

1 Like

Oh so basically i needed to create the variable in the module and then overwrite it. That’s nice.