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

[ SOLVED? ] How do I access Variables from another script?

I'm making a music playlist that can shuffle for a friend. I'm trying to figure out how to use a variable from another script, like global variables. I don't know how it works since I'm not that good at scripting. One of the scripts is local, the local script is the script I'm trying to use the variable from the other script. (* if that makes sense.* )

SCRIPT:

---------------------------------------------------------
-- MUSIC
---------------------------------------------------------

while true do
	
local table = {
	s1,
	s2,
	s3,
	s4,
	s5 -- the table of the sounds
}

local Randomizer = math.random(1,#table) -- the function that randomizes the items in the table

local Randomed = table[Randomizer] -- this is the value that was chosen from the randomizer

print(tostring(Randomed).." is currently playing.") -- prints which song is playing
Sound.SoundId = Randomed.SoundId -- transfers the song to the musicplayer
Sound:Play() -- plays the sound
wait(130) -- waits untill the song is over
Sound:Stop() -- stops the sound
Sound.SoundId = "http://www.roblox.com/asset/?id=" -- takes out the sound id
	
end

LOCAL SCRIPT:

--------------------------------------------------------
-- SONG NAME
--------------------------------------------------------

if music.IsPlaying then
	name = Randomed.SOUND_NAME -- me trying to use the variable from the other script
end

I found out an easier way to do it! Thank you for the help but I don't need any more help with this question.

3 comments

0

You can make use of a "RemoteEvent" if you want to send information to a local script.

0

I'm not really good at scripting and I don't know how a RemoteEvent works but I'll try!

0

the main thing you want. is FireAllClients

1 answer

Well, I solved the problem but I didn't use RemoteEvent or Global Variables, it looks like there was an easier way! Here's how I did it ( you don't have to read it all btw ):

Server Script:

---------------------------------------------------------
-- VARIABLES
---------------------------------------------------------

local GetSound = game.Workspace.Music_Playlist_v1.Sounds
local Sound = Instance.new("Sound") -- creates a new sound to play
local name = Instance.new("StringValue")

---------------------------------------------------------
-- SONGS
---------------------------------------------------------

local s1 = GetSound.Song1
local s2 = GetSound.Song2
local s3 = GetSound.Song3
local s4 = GetSound.Song4
local s5 = GetSound.Song5 -- these are how the script gets the sound to play

---------------------------------------------------------
-- PROPERTIES
---------------------------------------------------------

Sound.Parent = workspace

Sound.Name = "MusicPlayer"

name.Parent = Sound

name.Name = "SOUND_NAME"

---------------------------------------------------------
-- MUSIC
---------------------------------------------------------

while true do
	
local table = {
	s1,
	s2,
	s3,
	s4,
	s5 -- the table of the sounds
}

local Randomizer = math.random(1,#table) -- the function that randomizes the items in the table
	

	
local Randomed = table[Randomizer] -- this is the value that was chosen from the randomizer

print(tostring(Randomed).." is currently playing.") -- prints which song is playing
Sound.SoundId = Randomed.SoundId -- transfers the song to the musicplayer
Sound.Pitch = Randomed.Pitch -- copies the pitch of the audio
name.Value = Randomed.SOUND_NAME.Value -- pastes the name of the audio into the "music player"
Sound:Play() -- plays the sound
wait(130) -- waits untill the song is over
Sound:Stop() -- stops the sound
Sound.SoundId = "http://www.roblox.com/asset/?id=" -- takes out the sound id
Sound.Pitch = 0 -- resets the pitch
name.Value = ""
	
end

Local Script:

---------------------------------------------------------
-- VARIABLES
---------------------------------------------------------

local MP = script.Parent
local progress_bar = MP.PROGRESS_bar
local progress = MP.PROGRESS_bar.progress
local pause = MP.PAUSE_BUTTON
local volume = MP.VOLUME_BUTTON
local playing = MP.PLAYING_SONG
local name = MP.PLAYING_SONG.SONG_NAME
local music = game.Workspace.MusicPlayer
local name2 = music.SOUND_NAME
local test = Instance.new("Sound")

---------------------------------------------------------
-- PAUSE BUTTON
---------------------------------------------------------

function onClick()
	if pause.Image == "http://www.roblox.com/asset/?id=4666595627" then
		pause.Image = "http://www.roblox.com/asset/?id=4666594565"
		music:Pause()
	else
		pause.Image = "http://www.roblox.com/asset/?id=4666595627"
		music:Resume()
	end
end

pause.MouseButton1Click:Connect(onClick)

---------------------------------------------------------
-- VOLUME BUTTON
---------------------------------------------------------

function onClick2()
	if volume.Image == "http://www.roblox.com/asset/?id=4666200375" then
		volume.Image = "http://www.roblox.com/asset/?id=4666216912"
		music.Volume = 0
	else
		volume.Image = "http://www.roblox.com/asset/?id=4666200375"
		music.Volume = 1
	end
end

volume.MouseButton1Click:Connect(onClick2)

--------------------------------------------------------
-- SONG NAME
--------------------------------------------------------

while true do
if music.IsPlaying then
	name.Text = name2.Value
else
	name.Text = "..."
	end	
	wait(130)
end

If you didn't know, I'm making a music player that shuffles the music. I was having trouble creating the "song playing" script but eventually, I did it! Thanks for all the help though, I appreciate it.

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