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

Help with local music script?

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)

3 answers

✓ Accepted answer

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

2 comments

0

I understand what you mean, however when I try exactly what you have said, it does not even play the music at all.

0

Ah, nevermind. I tweaked my script a bit and found the problem. I appreciate your help. :)

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.

1 comment

0

I understand what you mean and repeated what you said, but it does not seem to be working still. I couldn't explain how it isn't working as there is no errors.

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)

1 comment

0

Sadly, this doesn't seem to work either. I have copied it exactly as you have and it still does not play the music.

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