I would Like to know
Are you able to Get Variables from other Scripts?
3 answers
There are a few ways I can think of to get variables from other scripts. I will list them below.
Global Variables
This, I think is the easiest of all of them, all you would have to do is add a _G. to the beginning of every variable used. However, it is not recommended to use this method.
First Script
_G.Hello = 'I am a Global Variable!'
Second Script
print(_G.Hello) -- >I am a Global Variable!
Bindable Functions
In one script, you can Invoke the object to return a preferred variable to you from another script. I think the code will simplify it (since I am not sure how to put it in basic terms).
First Script
Instance.new('BindableFunction',game.ServerStorage).Name = 'Bind' --This will set up the bindable function.
local Hello = 'I am a Regular Variable, I am being used by another script!' --Define the variable.
function game.ServerStorage.Bind.OnInvoke(--[[Tuple Arguments can be added here, may help if you want just one variable out of many.]])
--Since it's a callback, you must set up the function like this (either this or I have no way around it).
--Also, since this is a BindableFunction, we can return a value. So we will return that Hello value when asked to.
return Hello --This will return the value.
end
Second Script
print(game.ServerStorage.Bind:Invoke())
--The output should be >I am a Regular Variable, I am being used by another script!
Modules
Modules can basically store and return values in a table. I am not sure if you can edit these variables though and they would be heard script to script (then again, I have never tested it). All you would have to do is call for the script, and get the desired variable.
Module Script
local Var = { Hello = 'I am a Regular Variable, I am being used by another script!' }
return Var
Regular Script
Var = require(game.ServerScriptService.ModuleScript) --We'll just assume the module will be in there.
print(Var.Hello) --This will look through the table of variables you set up, and it will find '.Hello' then it should print >I am a Regular Variable, I am being used by another script!
I am sorry I did not go into more detail explaining things, I am not sure about some of these techniques being used throughout an entire script. If you understand it, then that's great, if you're confused about a particular area let me know.
5 comments
This is to access functions, but it's still useful to know.
Shared is a global table that can be used by any script of the same type as set. It can store any form of value a normal table can.
Make sure you mention that local scripts can't obtain global values set from the server
I did in the comment above yours, 'can be used by any script of the same type as set'
That's incorrect. The location has to be the same. A module script and regular script in the server aren't of the 'same set' yet they can still read and write the same global values.
Sure you can. There's a reason that Roblox has Value instances. These are able to store variables, and you can access them from any script. As an example, I'll show you how to get the players name from the LocalScript, then access it from the regular Script.
Say I have a StringValue, a Script, and a LocalScript, all inside of a player's Playergui.
Now, we'd be able to get the players name from the LocalScript by doing this:
Name=game.Players.LocalPlayer.Name --Gets name
script.Parent.StringValue.Value=Name --Puts name in StringValue
Now, here's how we would be able to get the name from the regular Script:
Name=nil --Sets the variable for name
repeat wait()
until script.Parent.StringValue.Value~="" --This checks to make sure that the players name is there.
Name=script.Parent.StringValue.Value --Sets the 'Name' variable to the players name.
Anyways, this is just one example of how you would be able to do this. It can also be done with Objects, Bools, and Numbers. If you have any further questions, please leave a comment below. Hope I helped clarify this for you. :P
1 comment
Not what I was looking for.
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
This has helped me so much it's more than Knowledge it's Truth.