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

How do i make a local sound play when a part is touched?

Asked by
VVoretex 146
4 years ago

I'm making a game with my friend, and I am trying to make something where when touched, it would play a sound locally, and I've tried to find this before, but I can't here is my touched script:

01Part = script.Parent
02Part.Touched:connect(function(HIT)
03    local H = HIT.Parent:FindFirstChild("Humanoid")
04    if H then
05    H.Torso.CFrame = CFrame.new(-136.965, -145.745, -366.13)
06            local Player = game.Players:GetPlayerFromCharacter(HIT.Parent)
07            Player.PlayerGui["Victory!Gui"].Victory.Visible = true
08 
09end
10    end)

1 answer

Log in to vote
1
Answered by 4 years ago

Hello. You must use a RemoteEvent and :FireClient(). First, insert a RemoteEvent into ReplicatedStorage and call it "FallEvent". Now, change your script to this:

01local Part = script.Parent
02 
03Part.Touched:Connect(function(HIT)
04    local H = HIT.Parent:FindFirstChild("Humanoid")
05    local Player = game.Players:GetPlayerFromCharacter(HIT.Parent)
06    if H and Player then
07    H.Torso.CFrame = CFrame.new(-136.965, -145.745, -366.13)
08    Player.PlayerGui["Victory!Gui"].Victory.Visible = true
09        game.ReplicatedStorage.FallEvent:FireClient(Player)
10 
11end
12    end)

The event will fire the client whenever a player touches it.

Now, insert a LocalScript into StarterPlayer > StarterPlayerScripts and insert the following code:

1game.ReplicatedStorage:WaitForChild("FallEvent").OnClientEvent:Connnect(function()
2    workspace.Sound:Play()
3end)

Whenever the FallEvent gets fired for the client, the sound in workspace (assuming you put it there) will play only for the client.

Please upvote and accept this answer if it helped.

Ad

Answer this question