local modulescript = {}
function modulescript.testFunction(var, var2, var3)
"CFrame Calculations requiring all the variables here"
end
In a script:
require(modulescript)
local var = POSITION PLACEHOLDER
local var2 = DIRECTION PLACEHOLDER
local var3 = MOUSE POSITION PLACEHOLDER
OnClientEvent:Connect(function()
modulescript.testFunction(var, var2, var3)
end)
I want to have a library of gun functions in the ReplicatedStorage as I’m planning to add a lot of weapons, and I don’t want to go through every single script whenever I discover a bug that needs to be fixed.
Is there a way to get variables from a script that required the modulescript inside the modulescript without cramming all the variables inside the arguments?
Here’s a TLDR of what I need:
1: script requires gun library that is inside the ReplicatedStorage
2: script calls for a fire function inside the library that needs multiple changing variables (like mouse position, world position and current position of the player) without cramming all the information inside the arguments
If you declare your variables in the global scope you can call getfenv() and pass in the environment table as a singular argument.
-- modulescript
local module = {}
function module.test(env)
print(env.var)
print(env.var2)
print(env.var3)
end
return module
-- script
local module = require(modulescript)
var = 1
var2 = 2
var3 = 3
module.test(getfenv())
But in my opinion it might just be better to put all your needed variables inside a table if you only want one parameter.
That way you can send the data you need instead of the entire global variable environment.
-- modulescript
local module = {}
function module.test(data)
print(data.position)
print(data.direction)
print(data.mouse)
end
return module
-- script
local module = require(modulescript)
local var = 1
local var2 = 2
local var3 = 3
module.test({
position = var,
direction = var2,
mouse = var3
})
You could make a table/dictionary with all the values and send it as a single parameter. Which is probably not what you’re looking for. You could really just end up making either a main module with all the variables inside that you’d be needing, or you could change the script that calls the function into a module and just read everything from there.