Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Music start playing for just one player?

Player touch part and song start playing doesn't work. The player must be the only one listing the music when he or she touch it, i mean like just their own not other players too only if they touch it too! Whats wrong? Am thankfull if someone can help me ;)

local sound = script.Parent.Sound 

game.Players.PlayerAdded:connect(function (newPlayer)

repeat wait() until script.Parent.Parent.Parent:IsA("Player")
	script.Parent.Touched:connect(function(p) 
    	local human = game.Players:GetPlayerFromCharacter(p.Parent)
    	if human and not sound.IsPlaying then
        sound:Play()
	end
end)
end)

1 answer

✓ Accepted answer

This code confused me a bit.

  • There's no point at all to use a PlayerAdded event for a touch script.
  • If script.Parent is a Part, how could script.Parent.Parent.Parent.Parent ever be a Player?
  • If 'sound' is inside the Part, then it must be a descendant of workspace. Therefore it won't play locally, but will play so that everyone can hear it.

But the actual touch part of your script is fine, although you'll want to make sure the sound is inside PlayerGui.

local sound = script.Parent.Sound

script.Parent.Touched:connect(function(p)
	local human = game.Players:GetPlayerFromCharacter(p.Parent)
	if human and not human:WaitForChild("PlayerGui"):FindFirstChild("Sound") then
		local newSound = sound:Clone()
		newSound.Parent = human.PlayerGui
		newSound:Play()
	end
end)

12 comments

0

The script have to be local and being into the part that the player touches? And the sound have to be in Startergui?

0

I don't understand that sentence. Touch scripts should never be local, because local scripts don't run in workspace. And why would sound be in StarterGui? Just leave it in the part, where you had it, because the script does the cloning into PlayerGui. Although you could put it in StarterGui, but we'd have to change some things in the script.

0

Ooh well, bc it doens't work ;/

0

It works. I tested it. You getting any output?

0

No i dont get any output it try differents ways its still not working..

0

Just put the Script and the Sound in the part of your choice.

0

Sorry, it works perfect i did add it in a wrong brick..

0

hmmm but how can i let it stop? and start a other song?

0

You just use the Stop() command, but that's a whole nother question, so accept this one if it helped please.

0

No problem i forgot too do sorry ;D

0

with the stop command the music keep going on and dont start the new one on the next part

0

Ask a new question on it with the new code you're using.

This discussion is preserved as it was. New answers, comments, and votes are closed.