Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Are you able to Get Variables from other Scripts? [closed]

Asked by
woodengop 1134 Moderation Voter
9 years ago

I would Like to know

Locked by Redbullusa and M39a9am3R

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

3 answers

Log in to vote
4
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
9 years ago
Edited 5 years ago

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.

0
This has helped me so much it's more than Knowledge it's Truth. woodengop 1134 — 9y
Ad
Log in to vote
2
Answered by
Muoshuu 580 Moderation Voter
9 years ago

Not without using shared (_G) or a value instance.

I assume you don't mean ModuleScripts.

0
This is to access functions, but it's still useful to know. dyler3 1510 — 9y
0
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. Muoshuu 580 — 9y
0
Make sure you mention that local scripts can't obtain global values set from the server DigitalVeer 1473 — 9y
0
I did in the comment above yours, 'can be used by any script of the same type as set' Muoshuu 580 — 9y
0
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. DigitalVeer 1473 — 9y
Log in to vote
0
Answered by
dyler3 1510 Moderation Voter
9 years ago

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

0
Not what I was looking for. woodengop 1134 — 9y