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