Basically I'm trying to make a relatively simple script that only plays the music for the person that touches the part. Nobody else. However, for some reason it does not work and I don't understand why. I appreciate any help given.
local touched script.Parent.Touched:connect(function() local s = Instance.new("Sound") s.Name = "Sound" s.SoundId = "http://www.roblox.com/asset/?id=337129937" s.Volume = 1 s.Looped = false s.Parent = game.Players.LocalPlayer wait(1) s:play() end)
Your script just has one mistake. When the Players touch it the sound will play for everyone. You have not told the script that play the sound to which player. The FindFirstChild()
and the hit
function comes in use. When the player touches the Part, The script will detect which Player has touched the Part using the FirstFindChild()
. It will detect the Player's name and play the music for that player.
script.Parent.Touched:connect(function(hit) local s = Instance.new("Sound") s.Parent = game.Players:FindFirstChild(hit.Parent.Name) s.Name = "Sound" s.SoundId = "http://www.roblox.com/asset/?id=337129937" s.Volume = 1 s.Looped = false wait(1) s:play() end)
Sorry if you did not understand what I meant as I am not good at explaining
Next time explain how it isn't working. Anyhow, I see your issue.
Your sound instance is inside workspace. The workspace interacts with all players, therefore playing a sound from workspace will play it for everybody on the server. The solution to this is to put the sound into the player and play it from there.
script.Parent.Touched:connect(function(hit) local s = Instance.new("Sound") s.Parent = game.Players:FindFirstChild(hit.parent.Name) s.Name = "Sound" s.SoundId = "http://www.roblox.com/asset/?id=337129937" s.Volume = 1 s.Looped = false wait(1) s:play() end)