Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

2 comments

0

use IntValues

0

which is ... how ?

1 answer

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)

2 comments

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?

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.