Grabbing vars from a different script

  1. I’m making a config script for one of my plugins and I don’t know how I would do so? I’m trying to make global vars so that my scripts can call the variable and use it without having to make new variables.
  2. Things like _G have not worked for me. Ideally, it is something like adonis or had admin config system that uses normal vars but anything works so long as it functions.
  3. I don’t think my scripts have to be shared in order to make global vars but I’m willing to try what works?
    Any help is appreciated! Thanks!
9 Likes

You could use a ModuleScript. ModuleScripts all you to use their contents inside other scripts.

example code

-- module script
local module = {}

module.MyValue = 10

return module -- whatever is returned in a module script is what the scripts will see
-- from another script
local moduleScript = require(ModuleScript) -- this is just for an example, the actual parameters would have to be the module script's directory in the explorer

print(moduleScript.MyValue) --> prints 10

--[[
  In this example, the script was able to access the "MyValue" property of the "module" table from the module script.
  This was because the ModuleScript, returned that table to the script 
]]

more examples

-- module script
return function() : number
   -- this returns a function for the scripts
   -- btw, you don't have to return a table, you can return any value or a function
   return #game.Players:GetPlayers() -- this returns the number of players in the GetPlayers method
end
-- another script
local numOfPlayers = require(ModuleScript)
-- the module script returned a function, so to use it, all you have to do is use it like one

print(numOfPlayers()) --> prints the number of players in the current game session

Use cases?

You can use module scripts as a script that holds configuration for other scripts to access. For example, you could make a module script that Sets and Retrieves DataStores instead having to write hundreds of lines of codes for it.

11 Likes

Modules cache their result and thus requiring the same module from other scripts will return the same table. This gives you the ability to have global variables

1 Like

In my experience _G. is less efficient, causes lag and is less favorable in general.

2 Likes

I didn’t, for the script, I making _G doesn’t work for some reason.

2 Likes

Didn’t work for me? Returns moduleScript as nil

What does the code you used look like

I just pasted the code you used to try it out first before implementing my variables and it said Unknown value Module script

Oh, you have to replace “ModuleScript” with the module script’s directory

-- example
local moduleScript = require(script.ModuleScript)

Still didn’t work. It failed to print 10 even with that.

Ok, can you paste the code, so I can know exactly what you put? It shouldn’t fail. Also, what does the output say?

The output said module script isn’t a valid member of workspace scripts. Also the scripts are the exact same as yours.

Ok, did you use WaitForChild? The error means that the module script didn’t load in yet, different objects in workspace don’t load immediately on start up.

You should parent the ModuleScript to ReplicatedStorage as that’s the norm but you can use different parents

So, your script should look something like this:

local moduleScript = require(game.ReplicatedStorage:WaitForChild("ModuleScript"))

You could also do what @njesk12 suggested

With the new addition of Attributes, you can also add a valid variable to the attributes section and grab it from another script anytime. They update immediately when changed and can even be connected to events when those values change.

2 Likes

I don’t think that will work for what I’m trying to make, will test it out though.