Sharing / Calling Variables Between Two Local Scripts

  1. What do you want to achieve? I’m creating a dialogue. I want to run the dialogue function once the value of variable A is set to true using a seperate local script than what the variable / value was originally declared in.

  2. What is the issue? The variable is declared in the “joinMenuScript” local script, although I want to detect it’s true / false value through a seperate local script, “steveDialogueScript”. The scripts are both local, and are both children of StarterGui.

  3. What solutions have you tried so far? I’ve looked at about 3 - 4 articles, and some were answers too specific for this issue – or were across local + server or server + server.

For a better example,

Local Script A

local example = true

Local Script B

if example then
  print("hello, world!")
end

You would be looking at BindableEvents, and or a .Changed Event if you are using a ValueBase or Attribute.

You can essentially Connect an Event in ScriptB to Detect Changes from ScriptA, which for example:

A:

Example.Value = true -- this value is now set to true, if we 
-- connect a .Changed Event, it will be able to fire on other Scripts

B:

Example.Changed:Connect(function(value) -- detects change from Example
    -- because its on the Client, the server will be unaware of the change
    -- and this will only fire on the cliebt
    if value then -- if our value is true
        print("Hello world!") -- fire code
    end
end)

For BindableEvents if you are Attempting to constantly share between both scripts, you would just have a a Reciever on both scripts (two separate BindableEvents).


Edit:

I do not recommend using _G as what is stated below under any circumstance. _G is infamous for being easily exploitable by exploiters, which is something you would like to prevent in your games.
As _G is shared globally within the Context Level its in, exploiters can make a simple change that can effect the entire use of the global variable.

2 Likes

I personally use _G to do this, though im not sure if its best practice to do so.

Example:

Script #1:

_G.Variable = true

Script #2:

if _G.Variable == true then

— Your code.

end

1 Like

Hmm, this doesn’t seem to work for me. Am I utilizing it wrong?

(I’ve declared all the variables such as playButton above, I just thought it’d be unnecessary to share.)

~Script A~

local playbuttonclicked = false

playButton.MouseButton1Click:Connect(function()
    _G.playbuttonclicked = true
end)

~Script B~

if _G.playbuttonclicked == true then
	print("works!")
end

Reference it like this:

_G.playbuttonclicked = false

playButton.MouseButton1Click:Connect(function()
    _G.playbuttonclicked = true
end)

~Script B~

if _G.playbuttonclicked == true then
	print("works!")
end

The easiest way is to use attributes!

--Local script A
Player:SetAttribute("Example", true)

--Local script B
if Player:GetAttribute("Example") then
end

--You can remove the attribute doing:
Player:SetAttribute("Example")
3 Likes

You could try

  • Attributes (these are like custom properties on an Instance)
  • Boolean instance
  • _G
  • Create a module for communicating between the two scripts
  • Just merge your scripts
2 Likes

The first loacl script in StarterGui

“joinMenuScript”

local playButton = joinMenuSgui.playButtonjoinmenu

Player = game.Players.LocalPlayer

Player:SetAttribute("playbuttonclicked", false)

playButton.MouseButton1Click:Connect(function()
	Player:SetAttribute("playbuttonclicked", true)
end)

The second local script in StarterGui

“steveDialogueScript”

Player = game.Players.LocalPlayer

if Player:GetAttribute("playbuttonclicked", true) then
	print("works")
end

There are no errors in the output, this simply just does not print “works”.

On the second local script (“steveDialogueScript”), you check if the player have “playbuttonclicked” attribute on join, you need to have a “listener” for when your att changes:

Second local script

local Player = game.Players.LocalPlayer

Player:GetAttributeChangedSignal("playbuttonclicked"):Connect(function()
      if Player:GetAttribute("playbuttonclicked", true) then
	     print("works")
       end
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.