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

Help with local music script?

Asked by 8 years ago

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

Log in to vote
0
Answered by 8 years ago

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

0
I understand what you mean, however when I try exactly what you have said, it does not even play the music at all. toughjohncena1256 57 — 8y
0
Ah, nevermind. I tweaked my script a bit and found the problem. I appreciate your help. :) toughjohncena1256 57 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

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.

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. toughjohncena1256 57 — 8y
Log in to vote
0
Answered by 8 years ago
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)

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. toughjohncena1256 57 — 8y

Answer this question