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:

Part = script.Parent
Part.Touched:connect(function(HIT)
    local H = HIT.Parent:FindFirstChild("Humanoid")
    if H then
    H.Torso.CFrame = CFrame.new(-136.965, -145.745, -366.13)
            local Player = game.Players:GetPlayerFromCharacter(HIT.Parent)
            Player.PlayerGui["Victory!Gui"].Victory.Visible = true

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

local Part = script.Parent

Part.Touched:Connect(function(HIT)
    local H = HIT.Parent:FindFirstChild("Humanoid")
    local Player = game.Players:GetPlayerFromCharacter(HIT.Parent)
    if H and Player then
    H.Torso.CFrame = CFrame.new(-136.965, -145.745, -366.13)
    Player.PlayerGui["Victory!Gui"].Victory.Visible = true
        game.ReplicatedStorage.FallEvent:FireClient(Player)

end
    end)

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

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

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

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