I need help making music play locally when a certain player touches a part?

I’m very new to scripting and I’m trying to make it so when a player touches a part, it plays music for only that player.

I noticed in my play testing that the music plays for all players.

I first put the sound in PlayerGui and later StarterPlayerScripts, but both do the same thing.

Here’s the script I’m using:

local debounce = false

function onTouched(part)
	if not debounce then
	debounce = true
	game.Players.LocalPlayer.PlayerScripts.Music.Sky1:Play()
	wait(1)
	debounce = false
	end
end

game.Workspace.MusicMaker5.Touched:connect(onTouched)

This script is a LocalScript and is located in StarterPlayerScripts. (Music is also located in StarterPlayerScripts in a model)

It plays the music as intended but the music plays for everyone.

Does anyone know the error here?

I believe you should try to use PlayLocalSound, which will only play the sound locally to the specific client, try it out and see if it works. It’s a function of SoundService

I’ve just tried this script:

local SoundService = game:GetService("SoundService")

local debounce = false

local function playLocalSound()
	if not debounce then
	debounce = true
	local sound = SoundService.Start
	SoundService:PlayLocalSound(sound)
	wait(1)
	debounce = false
	end
end

game.Workspace.MusicMaker1.Touched:connect(playLocalSound)

It plays the music, but once again it plays for everyone.
I put this script in StarterPlayerScripts, and the music is in Soundscape.

That’s odd, I can quickly go to a place where I made local audio and share the code if needed, one second

Why can’t you just make the Sound a new instance, and clone it into the player’s PlayerGui?

The music will play for everyone because you’re not checking if the intersecting part comes from a character and if so, which player the character belongs to and if that player is the same as the LocalPlayer. This makes the sound end up being pseudoglobal.

I made a resource addressing this problem some time back:

3 Likes

After some trial and error following your tutorial I finally got it to work! Thanks!