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

How to make a sound play for only the player that touched a seat?

You know how cars play startup chimes when you start them? I'm trying to make that happen when you touch the car's driving seat in my game. I hadn't the slightest idea how to do that, so I morphed together two scripts I found on here, but they don't work, maybe because they're out of date? Idk. Please comment any suggestions below.

script.Parent.Touched:connect(function(hit)
  if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
	        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        local plrgui = player.PlayerGui
        print(player)
	        local sound = script.Parent.Sound
	sound:Clone().Parent = plrgui
	        sound:Play()	    
	end
	end)

1 comment

1

Use a local script or an even to FireClient(player) and OnClientEvent()

2 answers

-- local script in starterplayerscripts
deb = false
script.Parent.Touched:Connect(function(hit)
	if not deb then
		deb = true
		local sound = 00000000 -- change this to your soundid
		sound:Play()
		wait(1.5)
		deb = false
	end
end)

the infamous client remote is here to help you always. Put this in a normal script:

local Part = workspace.Part
local Remote = game:GetService("ReplicatedStorage").Remotes.RemoteEvent
local Debounce = false
Part.Touched:Connect(function(PotentialPlayer)
    if PotentialPlayer.Parent:FindFirstChild("Humanoid") and game:GetService("Players"):FindFirstChild(PotentialPlayer.Parent.Name) then -- Checking if the Potetial Player has a Humanoid and their name is in the "Players" service
        if not Debounce then
            Debounce = true
            Remote:FireClient(game:GetService("Players"):GetPlayerFromCharacter(PotentialPlayer.Parent))
            wait(1.5)
            Debounce = false
        end
    end
end)

Put this in a Local Script:

local func = {}

function func:PlaySound(player, soundid)
    local newSound = Instance.new("Sound", script)
    newSound.SoundId = "rbxassetid://" .. soundid
    newSound:Play()
    newSound.Ended:Connect(function()
        newSound:Destroy()
        print'Sound has finished playing'
    end)
end

game:GetService("ReplicatedStorage").Remotes.RemoteEvent.OnClientEvent:Connect(function(player)
    func:PlaySound(player, 000000)
end)

Enjoy it. It works good, but bugs may appear. I tested it for myself and it works good.

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