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.
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.
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.
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.
--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")
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)