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

is there a way for a variable to be shared between scripts , not functions ?

Asked by 4 years ago
Edited 4 years ago

example variable's value is "0" , localscript from startergui updates varibale to "1" and script from workspace.part takes the variable's value for something It looks like something like this:

Local variable = "0" --I don't know where to put this

for script 1 ; Script location : game.StarterGui ~~ Script Type : Local script

variable = "1"

for script 2 ; Script Location : game.Workspace.Part

game.Workspace.Part.ClickDetector.MouseClick:Connect(function(Player)
Local Answer = variable + "1"
print(variable.."+ 1 = " .. Answer)
end)

the print should be "1 + 1 = 2

0
use IntValues maxpax2009 340 — 4y
0
which is ... how ? rupertrosse 39 — 4y

1 answer

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago

Lua is case sensitive, the datatype local must be defined with a lowercase L. You can also use the global array _G to store universal variables.

Lua will also perform the numerical operation with the two String expressions seen at line 2 of your second Script. This will result in a float 2.0 which will be concatenated into a result that looks like so: 2.0 + 1 = 2.0.

I also highly suggest you leave numerical operations to numerical datatypes.

_G.Variable = 1

workspace.Part.ClickDetector.MouseClick:Connect(function()
    local Number = _G.Variable
    print(Number.." + "..Number.." = "..(Number + Number)
end)
0
Is _G like local but the variable can be used in all scripts in the game?, if yes, then can you change the value of the variable? How? Is it like local or does it have its own code? rupertrosse 39 — 4y
0
Yes, it operates no differently from the basics of a variable, however it does have scope restrictions, meaning it cannot be local, it is always global. Ziffixture 6913 — 4y
Ad

Answer this question