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
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
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)