Play sound for the player that is sitting in a seat

I want to play a sound for the player that is sitting in the seat, and if the player is not sitting in that seat anymore, stop the sound for the player that sat in the seat.
i tried to script it but it doesn’t wanna work

heres the script:

local car1 = game.Workspace.Car.Chassis
local vehicleSeat = car1:FindFirstChild("VehicleSeat")

while task.wait(0.1) do
	if not vehicleSeat.Occupant then
		game.Workspace.Car.Effects.WindAmbient1:stop() --if no player is in seat stop playing the sound
else
		local player = game.Players:GetPlayerFromCharacter(vehicleSeat.Occupant.Parent)
		if player then
		game.Workspace.Car.Effects.WindAmbient1:Play() --play sound if player is in seat
	end
	end
	end

Im very happy if you guys could help me

2 Likes

What this is doing is constantly playing it, because we’re not doing a check for if it’s already playing. I believe calling :Play() on it will replay it if it’s a static Sound object and you’re not creating new ones.

I would use GetPropertyChangedSignal to get when the Occupant property changes. Something like this:

local car1 = game.Workspace.Car.Chassis
local vehicleSeat = car1:FindFirstChild("VehicleSeat")

vehicleSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
    local player = game.Players:GetPlayerFromCharacter(vehicleSeat.Occupant.Parent)
    if not player then return end

    if vehicleSeat.Occupant then
        game.Workspace.Car.Effects.WindAmbient1:Play() 
    else
        game.Workspace.Car.Effects.WindAmbient1:Stop()
    end
end)

This would work, but other people would be able to hear this. If you want only the player to hear this, you would need to create a RemoteEvent on the client (connecting a function using OnClientEvent which plays the sound on the client side, and then calling :FireClient() where we play the sound in the above script. This is how you would set that up:

LocalScript/Client:

local soundRemote = <LOCATION OF REMOTE EVENT OBJECT>
local sound = game.Workspace.Car.Effects.WindAmbient1

soundRemote.OnClientEvent:Connect(function(shouldPlay)
    if shouldPlay then
        sound:Play()
    else
        sound:Stop()
    end
end)

Script/Server:

local car1 = game.Workspace.Car.Chassis
local vehicleSeat = car1:FindFirstChild("VehicleSeat")
local soundRemote = <LOCATION OF REMOTE EVENT OBJECT>

vehicleSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
    local player = game.Players:GetPlayerFromCharacter(vehicleSeat.Occupant.Parent)
    if not player then return end

    if vehicleSeat.Occupant then
        soundRemote:FireClient(player, true)
    else
        soundRemote:FireClient(player, false)
    end
end)
1 Like

It works but the sound doesn’t stop when the player is not sitting anymore (the two scripts you gave me so that it only works for the player)

Put a few prints in both the client and server, specifically:

local soundRemote = <LOCATION OF REMOTE EVENT OBJECT>
local sound = game.Workspace.Car.Effects.WindAmbient1

soundRemote.OnClientEvent:Connect(function(shouldPlay)
    if shouldPlay then
        print("PLAY2") -- Here
        sound:Play()
    else
        print("STOP2") -- Here
        sound:Stop()
    end
end)

and

local car1 = game.Workspace.Car.Chassis
local vehicleSeat = car1:FindFirstChild("VehicleSeat")
local soundRemote = <LOCATION OF REMOTE EVENT OBJECT>

vehicleSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
    local player = game.Players:GetPlayerFromCharacter(vehicleSeat.Occupant.Parent)
    if not player then return end

    if vehicleSeat.Occupant then
        print("PLAY1") -- Here
        soundRemote:FireClient(player, true)
    else
        print("STOP1") -- Here
        soundRemote:FireClient(player, false)
    end
end)

Which prints actually print in the console/output? :slight_smile:

1 Like

Only play1 and play2 are printing

Oh of course, it’s because I check if the player is not nil above, and return if it is… No wonder it’s stopping lol.

Use this for the Server/Script instead:

local car1 = game.Workspace.Car.Chassis
local vehicleSeat = car1:FindFirstChild("VehicleSeat")
local soundRemote = <LOCATION OF REMOTE EVENT OBJECT>

vehicleSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
    local player = game.Players:GetPlayerFromCharacter(vehicleSeat.Occupant.Parent)

    if vehicleSeat.Occupant and player then
        soundRemote:FireClient(player, true)
    elseif not vehicleSeat.Occupant then
        soundRemote:FireClient(player, false)
    end
end)
1 Like

I get the same problem, the sound does not stop

Is the play prints still printing?

yes they are still printing

hoaokfsidfopkjkojfijiogasdfs

Is it only Play that’s printing and not a single stop at all? Where are your scripts located? What’s the explorer setup? :slight_smile:

I’ve just realised why it’s not stopping, because the player doesn’t exist at that point does it? So who do we send that remote event to? No one :joy:.

Use this for the Script/Server instead:

local car1 = game.Workspace.Car.Chassis
local vehicleSeat = car1:FindFirstChild("VehicleSeat")
local soundRemote = <LOCATION OF REMOTE EVENT OBJECT>
local playerThatWasOccupiedInVehicle = nil

vehicleSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
    local player = game.Players:GetPlayerFromCharacter(vehicleSeat.Occupant.Parent)
    if player and not playerThatWasOccupiedInVehicle then
        playerThatWasOccupiedInVehicle = player
    end

    if vehicleSeat.Occupant then
        soundRemote:FireClient(playerThatWasOccupiedInVehicle, true)
    elseif not vehicleSeat.Occupant then
        soundRemote:FireClient(playerThatWasOccupiedInVehicle, false)
        playerThatWasOccupiedInVehicle = nil
    end
end)

Note, this script would only work on a per-vehicle basis. You could easily set this up to work with multiple vehicles though. :slight_smile:

1 Like

Local script in StarterCharacterScripts or StarterPlayerScripts.
But if this machine is one, if there are a couple of them, then you need to do it a little differently.

local Car = game.Workspace:WaitForChild("VehicleSeat"):WaitForChild("Chassis")
local VehS = Car:FindFirstChild("VehicleSeat")
local Player = game.Players.LocalPlayer

VehS.ChildAdded:Connect(function(Who)
	if Who:IsA("Weld") then
		if Who.Part1.Parent.Name == Player.Name then
			game.Workspace.Car.Effects.WindAmbient1:Play()
		end
	end
end)
VehS.ChildRemoved:Connect(function(Who)
	if Who:IsA("Weld") then
		if Who.Part1.Parent.Name == Player.Name then
			game.Workspace.Car.Effects.WindAmbient1:Stop()
		end
	end
end)

It can be as simple as this:

local seat = workspace.Seat

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
    if seat.Occupant == nil then return end
    print(seat.Occupant.Parent.Name)
end)