Hey everyone! So I put together a Music Gui that basically is like a music control, it works fine and functions well but it only plays for me, and I wanted to know how I can make it so that it plays for everyone in the server!
Here’s the script (It’s a local script and I tried changing it to a normal script but doing that made it not play in the first place even for me):
local frame = script.Parent
local scrFrame = frame.ScrollingFrame
local music = frame.Music
local current = frame.Current
local volumeBar = frame.VolumeBar.VolumeGreen
local volumeUp = frame.VolumeUp
local volumeDown = frame.VolumeDown
local musicButton = frame.Parent.MusicButton
local stop = frame.Stop
local playing
frame.Visible = false
current.Text = "Currently Playing:"
local function playMusic(sound)
if playing then
playing:Stop()
end
sound:Play()
playing = sound
playing.Volume = tonumber(volumeBar.Text) / 100
end
volumeUp.MouseButton1Click:Connect(function()
if playing then
if playing.Volume < 1 then
playing.Volume += 0.1
end
volumeBar.Size = UDim2.new(playing.Volume, 0, 1, 0)
volumeBar.Text = math.floor(playing.Volume * 100 + 0.5)
end
end)
volumeDown.MouseButton1Click:Connect(function()
if playing then
if playing.Volume > 0 then
playing.Volume -= 0.1
end
volumeBar.Size = UDim2.new(playing.Volume, 0, 1, 0)
volumeBar.Text = math.floor(playing.Volume * 100 + 0.5)
end
end)
stop.MouseButton1Click:Connect(function()
if playing then
playing:Stop()
current.Text = "Currently Playing: "
end
end)
musicButton.MouseButton1Click:Connect(function()
frame.Visible = not frame.Visible
end)
for _, song in pairs(music:GetChildren()) do
local track = Instance.new("TextButton")
track.Parent = scrFrame
track.Text = song.Name
track.Font = Enum.Font.PatrickHand
track.TextScaled = true
track.MouseButton1Click:Connect(function()
current.Text = "Currently Playing: "..track.Text
playMusic(music[track.Text])
end)
end
Here is the layout of the objects related to the script:
I’m new to scripting and I would love if you guide me to what exactly I should do or replace since I’ve had this issue for a really long time and there isn’t any video explaining it so I figured I should put it here so it’s more specific!
Only localscripts work in a GUI. If you want to make a normal script do the functions, you’re going to need a RemoteEvent. This can be done simple, like this: LocalScript (Sender):
--send information from localscript
workspace.RemoteEvent:FireServer(param1, param2)
Script (Receiver):
workspace.RemoteEvent.OnServerEvent:Connect(function(player, param1, param2)
--code here manipulates the information from the event
--player is the parameter which contains the player instance that fired it
end)
You send information from a localscript to a serverscript.
Localscripts only work for that specific player, anything done with a localscript will be for only that player. You use a remote event to send a message from a localscript to a serverscript to play the music. Serverscripts work on the server and are used to communicate with everyone.
Firstly local scripts work only in a few folders, and normal scripts work in other folders. You cannot just put them anywhere you like. To do something in the client and let the server know you can use a remote event, Remote Functions and Events this will tell the server that you did something client sided.
You can detect the client-side from the server-side by using remoteEvent.OnServerEvent:Connect(function)
If you want to fire the event you will use, remoteEvent:FireServer()
Like functions you can also pass parameters, remoteEvent:FireServer(player)
oh alr! So what do i put exactly in those scripts? and also where do I put the line in the GUI’s script that sends the information to the server script
Also is the music gui only for admins, or does everyone have their own music gui?
If everyone has their own music gui, ignore the first reply I sent as that is not what you are looking for. Neither is the other replies from @daisytheghostchild98
Then yes, the first response I sent is correct. You want to fire an event to the server so that all players can receive it.
Firstly create a RemoteEvent in your ReplicatedStorage and name it PlayMusic
A script in ServerScriptService
local remoteEvent = ReplicatedStorage:WaitForChild("PlayMusic")
local function playMusic(music)
music:Play()
end
remoteEvent.OnServerEvent:Connect(playMusic)```
A local script wherever you put it in your GUI,
local function playMusic(sound)
if playing then
playing:Stop()
end
remoteEvent:FireServer(sound)
playing = sound
playing.Volume = tonumber(volumeBar.Text) / 100