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

How can a script access a variable thats in another script?

Asked by 6 years ago

I have a script that plays a song and when it does it has a variable called tempo. The tempo changes depending on the song. And I am trying to let tempo be accessed in another script but it says unknown global?

Theres the first script, tempo is only in the first block with Mr Blue Sky

local MrBlueSky = 862680756
local Believer = 685388224
local HowFarItGoes = 1058515832
local music = script.Parent
local musictitle = script.Parent.MusicName.MusicTeller.MusicTitle
local tempo = wait()

while true do
    wait()
    musicpicker = math.random(1,3)
    if musicpicker == 1 then
        musictitle.Text = "ELO - Mr Blue Sky"
        music.SoundId = "rbxassetid://"..MrBlueSky
        music:Play()
        music.PlaybackSpeed = 1
        music.Volume = 2
        tempo = wait(.3)
        music.Ended:wait()
        wait(3)
    end
    if musicpicker == 2 then
        musictitle.Text = "Imagine Dragons - Believer"
        music.SoundId = "rbxassetid://"..Believer
        music:Play()
        music.PlaybackSpeed = 1
        music.Volume = 2
        music.Ended:wait()
        wait(3)
    end
    if musicpicker == 3 then
        musictitle.Text = "Moana - How Far I'll Go"
        music.SoundId = "rbxassetid://"..HowFarItGoes
        music:Play()
        music.PlaybackSpeed = 0.673
        music.Volume = 2.5
        music.Ended:wait()
        wait(3)
    end
end
brick = script.Parent

while true do
    color = math.random(1,5)
    if color == 1 then
        brick.BrickColor = BrickColor.new(196/255, 40/255, 28/255) -- Bright Red
            wait(tempo)
    end
    if color == 2 then
        brick.BrickColor = BrickColor.new(4/255, 175/255, 236/255) -- Cyan
            wait(tempo)
    end
    if color == 3 then
        brick.BrickColor = BrickColor.new(52/255, 142/255, 64/255) -- Sea Green 
            wait(tempo)
    end
    if color == 4 then
        brick.BrickColor = BrickColor.new(123/255, 47/255, 123/255) -- Plum
            wait(tempo)
    end
    if color == 5 then
        brick.BrickColor = BrickColor.new(218/255, 133/255, 65/255) -- Bright Orange
            wait(tempo)
    end
end

Tempo is blue in this script and there both scripts not local scripts so I figured they could access each other. But they cant and I am a little confused on it.

0
The easy way to do this would be to just make a NumberValue so that both scripts are able to see it and set its value if necessary. Just be sure it's not somewhere that an exploiter can interact with it. Troidit 253 — 6y
0
AHHH NOT LOCAL VARIABLES THAT MAKE THE SCRIPT WASTE MORE TIME FINDING GLOBALS hiimgoodpack 2009 — 6y

1 answer

Log in to vote
2
Answered by
Troidit 253 Moderation Voter
6 years ago
Edited 6 years ago

There are two main ways to create a variable that other scripts can see.

The first and easiest method would be to make a NumberValue and put it somewhere easy for both scripts to access it. From there you could simply call it Tempo.

--Script 1
Tempo = script.Parent.Tempo

Tempo.Value = 1

So in script 2

--Script 2
Tempo = script.Parent.Tempo

print(Tempo.Value) -- outputs 1

On the other hand, you can also use the global table _G.

--Script 1

_G.Tempo = 1

So in script 2

--Script 2

print(_G.Tempo)

SOURCES http://wiki.roblox.com/index.php?title=Global_namespace/Basic_functions http://wiki.roblox.com/index.php?title=API:Class/NumberValue

0
Thanks my dude :) GottaHaveAFunTime 218 — 6y
0
why not BindableEvents? _G is actually deprecated (even tho the wiki doesn't show it), and things like NumberValues work well but sometimes they don't. Thundermaker300 554 — 6y
1
^Wrong. _G is NOT deprecated and it is on the wiki. http://wiki.roblox.com/index.php?title=Global_namespace/Basic_functions#G PyccknnXakep 1225 — 6y
0
Or, use modulescripts! Stop excluding dem, stop being mean D: hiimgoodpack 2009 — 6y
View all comments (2 more)
0
Yes, you can use module scripts too, they just seemed not very applicable to this situation. Troidit 253 — 6y
0
Oh lordy my question has strung up and argument... GottaHaveAFunTime 218 — 6y
Ad

Answer this question