Exit/Enter Vehicle

So I am trying to make a custom proximity prompt based enter/exit vehicle script. What’s supposed to happen is that if you interact with the proximity prompt, you get put into the vehicle and pressing space forces you to jump and teleports you outside of the vehicle. What is instead happening is that the enter system is working fine, but the exit vehicle script isn’t working.

Server Script:

local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")
local ExitCFrame = script.Parent.Parent.Body.ExitVehicleCFrame.CFrame
local Occupant
local PlayerFromCharacter

script.Parent:GetPropertyChangedSignal("Occupant"):Connect(function()
	Occupant = script.Parent.Occupant
	Occupant.JumpHeight = 0
	PlayerFromCharacter =  game.Players:GetPlayerFromCharacter(Occupant.Parent)
end)


-- ⚠️ The whole problem ⚠️
RS.VehicleFunctions.ExitVehicle.OnServerEvent:Connect(function(player) 
	if Occupant and PlayerFromCharacter and PlayerFromCharacter == player then
		Occupant.JumpHeight = 7.4
		Occupant.Jump = true
		Occupant.Parent.HumanoidRootPart.CFrame = CFrame.new(0.317, 3.467, -24.795)
	end
end)

Local Script in StarterPlayer < StaterPlayerScripts

local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer

UIS.JumpRequest:Connect(function(input, gameProcessed)
	RS.VehicleFunctions.ExitVehicle:FireServer(player)
end) 

How the exiting system works is that a script using the UserInputService is in the StarterPlayerScripts and it fires a remote event when the player jumps. Then when the server scripts detects the event being fired, it makes sure that the occupant of the vehicle matches the person who fired the remote event. After that it sets the occupant’s jump height to 7.4, which is a part of the problem, and then teleports them outside of the vehicle, which is the other problem.

Thanks!

EDIT:

Everything started working, so if you wish to use it you may. I have only used it on A-Chassis but you may try it on others if you wish.

For A-Chassis, create a script in the drivers seat and paste in the below.

local RS = game:GetService("ReplicatedStorage")
local ExitCFrame = script.Parent.Parent.Body.Color.ExitVehicleCFrame
local Occupant

script.Parent:GetPropertyChangedSignal("Occupant"):Connect(function(player)
	Occupant = script.Parent.Occupant
	Occupant = Occupant
end)

print(ExitCFrame.CFrame)

-- ⚠️ The whole problem ⚠️
RS.VehicleFunctions.ExitVehicle.OnServerEvent:Connect(function(player) 
	if Occupant == player.Character.Humanoid then
		task.wait(.1)
		player.Character:PivotTo(path.to.cframepart:WaitForChild("ItemWithCFrame").CFrame)
		player.Character.Humanoid.JumpHeight = 7.2
		player.Character.Humanoid.Jump = true
		print(player.Character)
	end
end)

Then create a script in StarterPlayerScripts and paste the below.

local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer

UIS.JumpRequest:Connect(function(input, gameProcessed)
	RS.VehicleFunctions.ExitVehicle:FireServer(player)
end) 

Finally, create a folder in ReplicatedStorage and name it VehicleFunctions, as well as put a remote event in that folder called ExitVehicle.

That’s it! You are done. Just make sure in the server script that you replace path.to.cframepart with the path to the CFrame Part and the ItemWithCFrame with the item that has the CFrame.

3 Likes

What do you mean by isn’t working? What happens when you jump?

The script is supposed to stop you from jumping, eject you out of the vehicle, teleport you to the desired location, and then put back your jump height to 7.4. What happens instead is that you just can jump and you never get teleported.

Your GetPropertyChangedSignal event runs when you make the player jump out of the seat, resulting in Occupant being nil. In your remote event code, you can use the player passed through in the remote event, get its character and do everything on that. Although Occupant.JumpHeight = 0 will still error when you make the player leave the seat.

Isn’t the default Jump thingy called JumpPower? you have to disable it to use JumpHeight

Nope, just checked in a new place and it was disabled by default.

Huh, in my place it had it enabled. Weird.

1 Like

Anyways, are there any errors in the Output? Maybe that will pin down the culprit.

I’ll try printing and see what the outcome is but I honestly don’t think so. This is because earlier in the script, when the player first gets into the seat, it sets the occupant to the player. Let me do a little testing real quick.

No, it actually doesn’t come back Nil. The humanoid belongs to the player, which in this case is TheKman2019. How we get the player is from taking the occupant which printed as “Humanoid” and doing :GetPlayerFromCharacter(), meaning it is detecting the occupant and getting the player.

image

It will become nil after you get the player out of the seat, because you are changing the Occupant property, therefore the event does indeed run, after you set Occupant.Jump to true. It won’t be nil when you fire the exit event.

Show your updated script.

Alright I made a lot of progress. Now it doesn’t error out and it does teleport when you give a CFrame position, although it doesn’t work if you get the CFrame of a part. What happens is that you just don’t get teleported to the part located outside of the car.

Updated Script:

local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")
local ExitCFrame = script.Parent.Parent.Body.Color.ExitVehicleCFrame
local Occupant

script.Parent:GetPropertyChangedSignal("Occupant"):Connect(function(player)
	Occupant = script.Parent.Occupant
	Occupant = Occupant
end)


-- ⚠️ The whole problem ⚠️
RS.VehicleFunctions.ExitVehicle.OnServerEvent:Connect(function(player) 
	if Occupant == player.Character.Humanoid then
		player.Character.Humanoid.JumpHeight = 7.2
		player.Character.Humanoid.Jump = true
		player.Character.HumanoidRootPart.CFrame = script.Parent.Parent.Body.Color:WaitForChild("ExitVehicleCFrame").CFrame
		print(player.Character)
	end
end)
1 Like

Could you try printing the part’s CFrame? You can also try pivoting the player’s model instead of setting the primarypart’s cframe, not sure if that’d change anything

It did indeed print the CFrame, which is below.

6.95737934, 3.08904696, -23.6904106, 0, 0, 1, 0, 1, -0, -1, 0, 0  -  Server - Jump:10

I also don’t know how to pivot a model, but I have tried to set the position of the player which also doesn’t work.

1 Like

you could try waiting .1 seconds after you make the player jump, then teleport them.

task.wait(.1)
player.Character:PivotTo(script.Parent.Parent.Body.Color:WaitForChild("ExitVehicleCFrame").CFrame)
1 Like

Sorry for the late response! This doesn’t work unfortunately, which I am very surprised with. I also tried putting the part into the workspace just to do a sanity check but that didn’t change anything.

1 Like

Alright so somehow I managed to fixed it by teleporting the player and then jumping them and setting their jump height? I have no idea how this works and I did it but it does, I ain’t complaining.

If you manage to see this post and want the horrible code that somehow works, the post will be edited soon showing the code.