Make a player sit when a car is spawned

I am trying to make a script so that when a car is spawned, the player’s character sits in the vehicleseat. The car spawn script is server-sided, and I’m wondering if there is a way to make the character sit from the server-sided script. This is the script:

local posa = game.Workspace.CarPosA

game.ReplicatedStorage:WaitForChild("CheckPrice").OnServerInvoke = function(player,NameOfCar)
	
	return game.ServerStorage.Cars:FindFirstChild(NameOfCar).Price.Value	
	
end

game.ReplicatedStorage:WaitForChild("SpawnCar").OnServerEvent:Connect(function(player,NameOfCar)
	local car = game.ServerStorage.Cars:FindFirstChild(NameOfCar):Clone()
	car:SetPrimaryPartCFrame(posa.CFrame)
	car.Parent = workspace.Vehicles
	car:MakeJoints()
	car.Name = player.Name.."'s "..NameOfCar
end)

I know I could do something like this in StarterCharacterScripts but it doesn’t work server-sided:

car.DriveSeat:Sit(Character.Humanoid)
3 Likes

Can someone help me please this has been up for a long time.

1 Like

Try teleporting player to the CFrame of the seat ^-^

This won’t work because I have my seats Disabled check set to true so that players cannot walk into the seat.(This still allows players to sit to drive through a script)

Oh… Maybe you should make them the occupant value member…? (Sorry if I spelt it wrong >->, I hope you know what do I mean

There is no way to set the occupant value through a script that I know of, is there?

I think there is, I am not very sure. (I am not in studio rn)

I just tried using Sit in a server script in studio and I was able to get in a vehicle while the VehicleSeat was disabled.

2 Likes

What did your script look like? Because this is what I would usually do in a startercharacter script but I dont know how to do it in a serverscript.

local players = game:GetService("Players")
local Character = players.LocalPlayer.Character
car.seat:Sit(Character.Humanoid)

You can just do this:

game.ReplicatedStorage:WaitForChild("SpawnCar").OnServerEvent:Connect(function(player,NameOfCar)
	local car = game.ServerStorage.Cars:FindFirstChild(NameOfCar):Clone()
	car:SetPrimaryPartCFrame(posa.CFrame)
	car.Parent = workspace.Vehicles
	car:MakeJoints()
	car.Name = player.Name.."'s "..NameOfCar
	car.seat:Sit(player.Character.Humanoid)
end)
3 Likes

Ok thanks! I’ll try it later when I get back on my pc.

1 Like

This makes the player sit in the seat, but it doesn’t give them control of the vehicle.

In that case, you’re gonna have to use a RemoteFunction instead of a RemoteEvent. When it’s invoked, run the code on the server but don’t make the player sit. Instead, return the seat so that the LocalScript can sit the player in the seat.

Client Code:

local seat = game.ReplicatedStorage.SpawnCar:InvokeServer(NameOfCar)
seat:Sit(humanoid)

Server Code:

game.ReplicatedStorage:WaitForChild("SpawnCar").OnServerInvoke = function(player,NameOfCar)
	local car = game.ServerStorage.Cars:FindFirstChild(NameOfCar):Clone()
	car:SetPrimaryPartCFrame(posa.CFrame)
	car.Parent = workspace.Vehicles
	car:MakeJoints()
	car.Name = player.Name.."'s "..NameOfCar
	return car.seat
end
3 Likes

Would I put the client code in StarterCharacterScripts?

No, put the code where you fired the SpawnCar event. Also replace the SpawnCar event with a RemoteFunction named SpawnCar.

2 Likes

You can use :Sit() function. Read about it here.

In this case, the Sit function should be used on the client to enable vehicle control.

1 Like