I am trying to make a script that changes the background music when touching a part. The part being touched is the destination part of a teleporter. For some reason the music will not change at all. I am very new to scripting so I can’t figure out what’s wrong with this script. I tried looking online but I didn’t find anything that worked for me. Here is my script which is a Local Script in StarterPlayerScripts.
local SoundService = game:GetService("SoundService")
local hubworld = SoundService.HubBGM
local worldselect = SoundService.WorldSelectBGM
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
repeat wait(3) until game:IsLoaded()
if game:IsLoaded() then
hubworld:Play()
end
while game:IsLoaded() do
player.Touched:Connect(function(part)
if part.Name == "houseenter" or part.Name == "hubworldenter" and not hubworld.IsPlaying then
hubworld:Play()
worldselect:Stop()
end
if part.Name == "worldselectenter" and not worldselect.IsPlaying then
hubworld:Stop()
worldselect:Play()
end
end)
end
Please tell me why it won’t work. If you need to know more just let me know
It happened to my game as well. I used the code similar to mines, the music didn’t work out and it won’t play any music. So I used Region3 and it worked perfectly out.
There isn’t a Touched event for the Player object, which is one of the reasons why it isn’t working; that event currently only exists for BaseParts and Humanoids.
Because client-sided scripts that are placed into the StarterPlayerScripts are only cloned to the player’s PlayerScripts container once (upon joining the game), the variables at the top of the script will only be assigned a value once.
This means that the character variable will only reference that player’s first Character model and it will never be updated when they respawn unless there was code that updated the variable after Player.CharacterAdded fired, or if the script restarted from the beginning (which would automatically happen if the script was placed into StarterCharacterScripts, since scripts in that container will automatically be added to a player’s Character model every time they respawn).
It wouldn’t be necessary to include events like these inside of an infinite loop because that would be creating thousands of event connections that would degrade the performance of the game over time.
Instead, only one event would need to be connected to either the Humanoid (which would not be recommended in this use case since that event would be firing almost all the time), or, the preferred solution would be to have an event connected for each of the BaseParts that the player’s Character would need to interact with in order to play and stop the sounds (which seems like it would be the ones that are named houseenter, hubworldenter, and worldselectenter, based on what was included in the original post). This way, the events would only fire when something interacts with those specific parts, rather than every time the BaseParts within the Character model touched anything in the Workspace.
By chance, are there only 3 parts in total with those names, or are there many parts that have those names? If there are a lot of parts with those names, you could use the CollectionService to apply tags to parts that have those names so it’ll be easier to connect an individual event for each of the parts.
After knowing whether or not there’s just a few of those parts in the Workspace or a ton of them, I could provide an example revision of the original code, and then I could explain how it works, if you’d like.