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

Music start playing for just one player?

Asked by 9 years ago

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

Log in to vote
0
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

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)

0
The script have to be local and being into the part that the player touches? And the sound have to be in Startergui? minetrackmania 186 — 9y
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. Perci1 4988 — 9y
0
Ooh well, bc it doens't work ;/ minetrackmania 186 — 9y
0
It works. I tested it. You getting any output? Perci1 4988 — 9y
View all comments (8 more)
0
No i dont get any output it try differents ways its still not working.. minetrackmania 186 — 9y
0
Just put the Script and the Sound in the part of your choice. Perci1 4988 — 9y
0
Sorry, it works perfect i did add it in a wrong brick.. minetrackmania 186 — 9y
0
hmmm but how can i let it stop? and start a other song? minetrackmania 186 — 9y
0
You just use the Stop() command, but that's a whole nother question, so accept this one if it helped please. Perci1 4988 — 9y
0
No problem i forgot too do sorry ;D minetrackmania 186 — 9y
0
with the stop command the music keep going on and dont start the new one on the next part minetrackmania 186 — 9y
0
Ask a new question on it with the new code you're using. Perci1 4988 — 9y
Ad

Answer this question