How to make a solid car, that the player can jump out of?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    I want to be able to make a car that is solid (CanCollide), that the player can enter by pressing ‘E’ (This is already achieved), that the player can also jump ‘Space’ out of, without being stuck inside the car, or making the car fly uncontrolled into the air as a result of teleporting the player out of the car to an above location of +15 on the y axis.

  2. What is the issue?
    When I press ‘Space’ to jump out of the car (or the VehicleSeat) the player is stuck inside the car, or the car fly uncontrollably into the air.

  3. What solutions have you tried so far?
    I have looked every where for solutions, and I have found nothing but vague entries on the forum.
    So far I have tried having a localscript and a server script.
    The localscript is in charge of allowing the player to press E to enter the car and ‘Space’ to get out of the car. This is done by events that are fired to the server - it looks like this

local inputservice = game:GetService("UserInputService")

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

inputservice.InputBegan:connect(function(i,g)
	if i.UserInputType == Enum.UserInputType.Keyboard then
		if i.KeyCode == Enum.KeyCode.E then
			for _,Car in pairs(workspace.Vehicles:GetChildren()) do
				local Mag = (Car.DriveSeat.Position - Character.HumanoidRootPart.Position).magnitude
				if Mag <= Car.Configurations.Range.Value then
					Car.Event:FireServer()
					break
				end
			end
		end
		if i.KeyCode == Enum.KeyCode.Space then
			for _,Car in pairs(workspace.Vehicles:GetChildren()) do
				local Mag = (Car.DriveSeat.Position - Character.HumanoidRootPart.Position).magnitude
				if Mag <= Car.Configurations.Range.Value then
					Car.ExitCar:FireServer()
				break
				end
			end
		end
	end
end)

The server script is in charge of handling the events, and respectively teleports the player into the car and sets the player as the current driver & teleports the player out of the car (The latter is where the issue is presented. The car flies uncontrollably into the air. I have tried to circumvent this by setting the car parts ‘CanCollide’ to false, but no luck)
The script looks like this

local car = script.Parent
local HasDriver = false
local Driver

car.Event.OnServerEvent:Connect(function(Player)
	if Player and Player.Character then
		if (Player.Character.HumanoidRootPart.Position-car.DriveSeat.Position).magnitude <= car.Configurations.Range.Value then
			if not HasDriver then
				car.DriveSeat:Sit(Player.Character.Humanoid)
			end
		end
	end
end)

car.ExitCar.OnServerEvent:Connect(function(Player)
	print("Received Exit Car Event")
	if Player and Player.Character then
		print("Player and Player Character checked true")
		local occupyingPlayer = game.Players:GetPlayerFromCharacter(car.DriveSeat.Occupant.Parent)
		if HasDriver and occupyingPlayer == Player then
			print("Car has driver and the occupant is the player")
			car.Body.Parts.Base.CanCollide = false
			car.Body.Parts.Subbase.CanCollide = false
			car.Body.Parts.Windows.CanCollide = false
			
			Player.Character.Humanoid.Sit = false
			local TargetPosition = CFrame.new(Player.Character.HumanoidRootPart.Position - Vector3.new(0,-15,0))
			Player.Character.HumanoidRootPart.CFrame = TargetPosition
			
			car.Body.Parts.Base.CanCollide = true
			car.Body.Parts.Subbase.CanCollide = true
			car.Body.Parts.Windows.CanCollide = true
		end
	end
end)

car.DriveSeat.Changed:connect(function(property)
	if property == "Occupant" then
		if car.DriveSeat.Occupant then
			local player = game.Players:GetPlayerFromCharacter(car.DriveSeat.Occupant.Parent)
			if player then
				HasDriver = true
				car.Body.Parts.Base.CanCollide = false
				car.Body.Parts.Subbase.CanCollide = false
				car.DriveSeat:SetNetworkOwner(player)
				local localCarScript = script.LocalCarScript:Clone()
				localCarScript.Parent = player.PlayerGui
				localCarScript.Car.Value = car
			end
		else
			HasDriver = false
		end
	end
end)

I still need help with this by the way …

You could make it so when the player exits the car, make the roof’s cancollide false for a split second and give the player enough time to get out of the car in a LocalScript so it’s a client-sided effect.

1 Like

but that’s what I did, please take a look in the script. I set the parts ‘cancollide’ to false, then back to true.

Been trying all day to search solutions on Roblox Devforum and trying to implement solutions myself.
I can not make the player jump out of the car, once the player is inside due to the car having ‘CanCollide’ enabled.
Setting ‘CanCollide’ to false before the teleport of the player, and then back to true, does not work for me.
Any solutions? I’m really frustrated :frowning:

Could you send a video of the problem whenever you jump?

What are you attempting to do specifically in these lines? What is the significance of the Vector3 15 studs below where the HRP currently sits? Genuine question because if you focus solely on changing the CFrame of the player and “teleporting” them out of the car, then you don’t have to ever mess with the vehicle’s CanCollide property.

You could try replacing those lines with this line of code to see if it changes anything:

Player.Character:SetPrimaryPartCFrame(Player.Character.PrimaryPart.Position - Vector3.new(0,-15,0))

EDIT: I also just noticed that you’re subtracting a negative vector from the position instead of adding a positive vector, is there a reason for that?

I’m attempting to teleport the player with those lines.
I read on the forums that you have to use the CFrame to teleport, otherwise you could risk killing the player.

I added -15 because when I added 15, it didn’t teleport me upwards.

I tested your line of code, and it doesn’t seem to work.

Edit: I finally found a solution.
Instead of all the other code, a simple script under the seat is all that is needed and it does the trick.

local seat = script.Parent
local player

seat.ChildAdded:connect(function(child)
	print(child.Name)
	if child.Name == "SeatWeld" then
		player = child.Part1.Parent
		local plr = child.Part1.Parent:GetChildren()
	end
end)
seat.ChildRemoved:connect(function(child)
	if child.Name == "SeatWeld" then
		local plar = player:GetChildren()
		for i=1,#plar do
			local TargetPosition = CFrame.new(player.HumanoidRootPart.Position - Vector3.new(0,-5,0))
			wait(.1)
			player.HumanoidRootPart.CFrame = CFrame.new(TargetPosition.Position)
			break
		end
	end
end)