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

Sound playing for every player, not just the player who triggers the sound?

Asked by
MexheCo 46
6 years ago

I want to play a sound for the player that collides with the goal. It works, but it plays for every player in the game, not just the colliding player.

Here is my script:

script.Parent.Touched:Connect(function(hit)  --Use :Connect(), :connect() is deprecated.
 if hit.Parent:FindFirstChild('Humanoid') and game.Players:GetPlayerFromCharacter(hit.Parent) then 

  hit.Parent.Head.CFrame = CFrame.new(-118.926, 359.897, 18.574)
  hit.Parent.Humanoid.WalkSpeed = 16
  game.StarterGui.LevelComplete:Play()
wait(3)

end
end)

Also, I should probably note that the sound plays while in Studio, but not in Server testing.

Thanks, Mexhe

0
remote events local scripts yadda yadda go look up filtering enabled radusavin366 617 — 6y

2 answers

Log in to vote
0
Answered by
Cjjdawg -9
6 years ago

You should use a remote event that triggers when the user touches the brick. To do this first go to replicated storage and right click and insert a remote event and name it whatever you want. Assuming that your script is a server script, you will have to use fire client(which is the player). To trigger the remote event use this code format.

game.ReplicatedStorage.RemoteEventName:FireClient(player) -- Triggering the remote event

This should be placed in the serve script to fire the even. The parameter passed is the player

Then in the local script you would create the listening event for the remote event.

local plr = game.Players.LocalPlayer.Character
local function PlayMusic () --The parameter is not needed as with server to client communication the player firing the event is automatically passed
    game.StarterGui.LevelComplete:Play()
end()
game.ReplicatedStorage.RemoteEventName.OnClientEvent:Connect(PlayMusic) --Listening event for the firing of the remote event    
0
The local script can be place starter player scripts. But it must be placed in a location that will be copied to the starter player or it will not work. Because all local scripts that will be executed on a player must be parented to that player Cjjdawg -9 — 6y
0
in the second code end() should be end) typo sorry Cjjdawg -9 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
function playCompleteSound(player)
    if player then 
        local soundClone = player.PlayerGui:FindFirstChild("LevelComplete")  or game.StarterGui.LevelComplete:Clone()
        if soundClone.Playing == false then 
            soundClone.Parent = player.PlayerGui
            soundClone:Play()
        end
    end
end

script.Parent.Touched:Connect(function(hit)  --Use :Connect(), :connect() is deprecated.
    if hit then 
        if game.Players:FindFirstChild(hit.Parent.Name) then 
            local player = game.Players:FindFirstChild(hit.Parent.Name)
            if player.Character ~= nil then 
                local character = player.Character
                character.Head.CFrame = CFrame.new(-118.926, 359.897, 18.574)
                character.Humanoid.WalkSpeed = 16
                playCompleteSound(player)
            end
        end
    end
end)
0
Instead of playing the sound directly from the startergui, clone it and set it's parent in the player's PlayerGui. awesomeipod 607 — 6y

Answer this question