Remote Events playing music

Hi, so I am relatively new to scripting and am attempting to set up a system for players to let their songs be played in the game in a certain area. It is controlled using a GUI and includes local scripts. So I have used remote events. The only issue is the song will only change on the player’s side and not anyone else’s. This is what the scripts look like.

Local Script:
local music = game:GetService("ReplicatedStorage").Music
local IDN = script.Parent.Parent.TextBox.Text

script.Parent.MouseButton1Click:Connect(function()
    music:FireServer(IDN)
end)

music.OnClientEvent:Connect(function(IDN)
    local Sound = workspace.BeachSpeaker.Sound
    Sound.SoundId = "rbxassetid://" .. IDN
    Sound:Play()
end)

Server Script:
local music = game.ReplicatedStorage.Music

music.OnServerEvent:Connect(function(IDN)
    music:FireAllClients(IDN);
end)

Any help will be greatly appreciated! :grinning:

1 Like

OnServerEvent takes the first argument as the player. So do this.

music.OnServerEvent:Connect(function(player, IDN)
    music:FireAllClients(IDN)
end)

Also, whenever you provide code, PLEASE preformat it. It makes it much easier to read. You can do this by highlighting your code, then click the </> button.

After adding the changes you suggested, I am now getting the error message of image

1 Like

Are you sure you’re putting in a valid audio ID?

(Nevermind i took another look at your script lol) My bad. Make sure ID is correct.

You’re indexing the text from the TextBox before it’s actually modified by the user – this variable isn’t dynamic meaning it does not auto-update.

-- localscript

local music = game:GetService("ReplicatedStorage").Music

script.Parent.MouseButton1Click:Connect(function()
    music:FireServer(script.Parent.Parent.TextBox.Text) -- direct reference to the input
end)

music.OnClientEvent:Connect(function(IDN)
    local Sound = workspace.BeachSpeaker.Sound
    Sound.SoundId = "rbxassetid://" .. IDN
    Sound:Play()
end)

You also may want to add validation on both the server and client to make sure the ID isn’t blank.

2 Likes

Well. He has a button that has to be clicked. So I assume the player enters ID and then presses button. So text has to be updated. Oh, wait I see what you mean. @ryantubelive you have to get the text in the mouse click function since you need it to be up to date.

Instead you do this
music:FireServer(script.Parent.Parent.TextBox.Text)

You also dont need the IDN variable at the top.

1 Like

I saw you modified your post but I’ll explain it a little more in depth for clarification.

On run-time the IDN variable is being set to an empty string (that’s the default value of the textbox.) When the left mouse button is clicked you’re firing the remote and passing IDN as a variable, which is still empty because the reference is never updated to the new value of the textbox.

Yea, my brain malfunctioned lol. Sorry about that.

Thank you both so much for all the help! Have a good Christmas!