Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.