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

How can I change the background music once a specific player joins the game?

Asked by 3 years ago
Edited 3 years ago

I don't know much of scripting, and I've been having some troubles figuring this out. I've been trying to make the background music change to a different audio once a specific player joins the game, and the music returns back to the original audio once the player leaves. I've seen questions similar to this on this website before, but not exactly what I'm looking for. May I have some help?

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

You can use Players service's PlayerAdded event on a server script. Something like:

local Sound = (sound here);
game:GetService("Players").PlayerAdded:Connect(function(plr)
    if plr.Name ~= "player here" then return; end
    Sound:Stop();
    Sound.SoundId = "id here";
    Sound:Play();
end)

EDIT: Fixed dumb space :)

0
Doesn't seem to be working, what do I put in "sound here"? And don't you put the player's name in "player here", or the ID? SquishyGhastly 0 — 3y
0
"player here" is the player's name you want. If you want the ID, you can change to plr.UserId ~= "(id goes here)". In the "sound here" you put the reference to the [sound](https://developer.roblox.com/en-us/api-reference/class/Sound) instance. If you want you can do Instance.new("Sound"). aurich98 86 — 3y
0
You should always be using the UserId. It is a static UID. The Player's Name can be changed at their leisure, which makes it an unreliable affirmation. Ziffixture 6913 — 3y
0
I've did what you said, nothing worked. I messed around a few things, and still didn't work. I seriously have no idea what I'm doing wrong. SquishyGhastly 0 — 3y
0
Check the new awnser then aurich98 86 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

Hello,

You should put this code in a Server Script. Remember to use the Player ID and Sound ID. Otherwise it will not work.

local Sound = Instance.new("Sound")
Sound.Parent = game.Workspace

game:GetService("Players").PlayerAdded:Connect(function(player)
    if player.UserId == "PLAYERID" then
        Sound:Stop();
        Sound.SoundId = "rbxassetid://20912039"; -- Change ID to whatever sound you want to play when that player joins
        Sound:Play();
    else end
end)

Answer this question