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:

local finishPart = game.Workspace.Levels.Level1.Checkpoints:WaitForChild("Finish")

function onTouch (hit)
    local H = hit.Parent : FindFirstChild("Humanoid")
    local S = game.Players.LocalPlayer.PlayerGui.Ding
    if H ~= nil then
        S:Play()
    end
end


finishPart.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

local finishPart = game.Workspace.Levels.Level1.Checkpoints:WaitForChild("Finish")
local plr = game.Players.LocalPlayer
local char = plr.Character
repeat
    char = plr.Character
until 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

function onTouch (hit)
    local H = hit.Parent : FindFirstChild("Humanoid")
    local S = game.Players.LocalPlayer.PlayerGui.Ding
    if H ~= nil and game.Players.LocalPlayer.Character.Humanoid == H then
        S:Play()
    end
end


finishPart.Touched:connect(onTouch)

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

local finishPart = game.Workspace.Levels.Level1.Checkpoints:WaitForChild("Finish")
local plr = game.Players.LocalPlayer
local char = plr.Character
repeat
    char = plr.Character
until char ~= nil

finishPart.Touched:Connect(function(hit)
    if hit and hit.Parent:FindFirstChild("Humanoid") and hit.Parent == char then
        game.Players.LocalPlayer.PlayerGui.Ding:Play()
    end
end)

^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:

local 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
local SP = workspace:WaitForChild("What is the part name you put it here")

function onTouch (hit)
    local H = hit.Parent : FindFirstChild("Humanoid")
    local S = game.Players.LocalPlayer.PlayerGui.Ding
      if H then
        S:Play()
    end
end


SP.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