Can I define a variable in a script and use it in a module script and will it work if I require it in the script

So say if I had the MousePos as a variable in a script and I use that variable in a module script and then required it using that script that has the MousePos variable would it work?

Not sure if I explained that very well.

1 Like

Yes, I think it will work, most likely.

2 Likes

Good to know, I guess we’ll find out.

1 Like

You can use the shared global.

 shared.Pos = MousePos
 -- through another script
 print(shared.Pos)

Note that you can’t be sure whatever you assign to shared will be set before you access it, use a BindableEvent or some other mechanism to ensure the value was set.

You could also use a function in the module to test your theory

 return {
         returnPosition = function(self, position)
         return position
      end
 }

 -- script

 local module = require(path)
 local p = module:returnPosition(Vector3.new(1,2,3))
 print(p) -- 1,2,3

this is just a function that returns the passed value, did you mean something like this?

 local var = 5
 local module = require(path)
 -- in the module you set a variable with the same name to something else
 print(var) --> Will not print the new value
2 Likes

So I have a local script that sends the mouse pos to the script and then I need to get the mouse pos with the module script which is the best way to do that im kind of confused with your scripts

1 Like

You can carry parameters over to a module script.

--Script
local text = 'Hello world!'
local module = require(game.ServerStorage.ModuleScript)
module.printFunc(text)


--Module Script
local module = {}

module.printFunc = function(text)
       print(text)
end
1 Like

Why do you need to pass the mouse position to a module script?
Don’t use one if it’s unnecessary, minimize and simplify code where possible.

1 Like

Not sure, just to make it look tidy ig