You can write your topic however you want, but you need to answer these questions:
-
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. -
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. -
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)