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

[SOLVED] How do I make it so when a player touches a part a sound plays only for that player?

Asked by 4 years ago
Edited 3 years ago

Information:

  • I am using a LocalScript
  • I am trying to do play a sound once the player touches a part.
  • I want to play the sound for only the player that touched the part.
  • The sound is in StarterGui
  • The LocalScript is in StarterGui
  • Currently, it plays for both players.

Script:

01local finishPart = game.Workspace.Levels.Level1.Checkpoints:WaitForChild("Finish")
02 
03function onTouch (hit)
04    local H = hit.Parent : FindFirstChild("Humanoid")
05    local S = game.Players.LocalPlayer.PlayerGui.Ding
06    if H ~= nil then
07        S:Play()
08    end
09end
10 
11 
12finishPart.Touched:connect(onTouch)
0
Local scripts *only* work in StarterPlayerScripts, StarterPack, StarterGui or StarterCharacter so you can't put it into the part itself, change all references of the part to something like workspace:WaitForChild("Part") imKirda 4491 — 4y
0
@imKirda I edited to what you told me to do. The sound plays. But, it plays for everyone in the server, not just the player that touched the part. Please help. Dave_Robertson 42 — 4y

3 answers

Log in to vote
1
Answered by 4 years ago

ok so i have an answer for why it would play for all players, so each player has that script running for them, meaning that when one person touches the part every other script sees that someone touched that part and plays the sound, you need to tell it to only play the sound if YOU touch it, not anyone else

01local finishPart = game.Workspace.Levels.Level1.Checkpoints:WaitForChild("Finish")
02local plr = game.Players.LocalPlayer
03local char = plr.Character
04repeat
05    char = plr.Character
06until char ~= nil -- the reason why i do this is because the player's character might not be loaded in yet so it  repeats this until it is loaded in
07 
08function onTouch (hit)
09    local H = hit.Parent : FindFirstChild("Humanoid")
10    local S = game.Players.LocalPlayer.PlayerGui.Ding
11    if H ~= nil and game.Players.LocalPlayer.Character.Humanoid == H then
12        S:Play()
13    end
14end
15 
16 
17finishPart.Touched:connect(onTouch)

this will make it so it checks to see that the person who touched it, is the character

01local finishPart = game.Workspace.Levels.Level1.Checkpoints:WaitForChild("Finish")
02local plr = game.Players.LocalPlayer
03local char = plr.Character
04repeat
05    char = plr.Character
06until char ~= nil
07 
08finishPart.Touched:Connect(function(hit)
09    if hit and hit.Parent:FindFirstChild("Humanoid") and hit.Parent == char then
10        game.Players.LocalPlayer.PlayerGui.Ding:Play()
11    end
12end)

^this is the way i would do it, they both should work the same way though tell me if there are any questions or this doesn't work

hope this helps!

0
Worked perfectly! Thank you so much! Dave_Robertson 42 — 4y
0
glad i could help :D botw_legend 502 — 4y
Ad
Log in to vote
0
Answered by
Soban06 410 Moderation Voter
4 years ago
Edited 4 years ago

Try to change this:

1local S = --  locate the sound inside the startergui.

When there is something inside the startergui, it only replicated for that specific user. So placing the sound inside the startergui will make the sound play only for the player who touched the brick. Make sure you are referencing the sound correctly inside the local script.

0
I don't understand. Can you please elaborate? I am trying to play the sound for the player who touched the part, not for all players. The sound is game.StarterGui.Ding. The reason I didn't put game.StarterGui.Ding is because I want it only to play for the player that touched the part. Dave_Robertson 42 — 4y
0
If it is in the StarterGui, then you are all good. The sound should only play for the local player. Soban06 410 — 4y
0
Try to use relative paths like this: script.Parent. Not: game.Players.LocalPlayer.PlayerGui. Soban06 410 — 4y
Log in to vote
0
Answered by 4 years ago
01local SP = workspace:WaitForChild("What is the part name you put it here")
02 
03function onTouch (hit)
04    local H = hit.Parent : FindFirstChild("Humanoid")
05    local S = game.Players.LocalPlayer.PlayerGui.Ding
06      if H then
07        S:Play()
08    end
09end
10 
11 
12SP.Touched:connect(onTouch)

if that doesn't work tell me

0
The sound plays. But, it plays it for all players. Dave_Robertson 42 — 4y

Answer this question