So im trying to make a system like jailbreak so whenever you get near any seat (driver or passenger) it appears an e button that you press to enter and exit.
I took a code from okeansky that works only for vehicle seat and i would like to know if someone have idea to make it work in normal seats too.
video link:
code:
local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local runService = game:GetService("RunService")
local userInputService = game:GetService("UserInputService")
local carEnterGui = replicatedStorage.CarEnterGui
local character = players.LocalPlayer.Character
local closestVehicle, gui, currentSeat
character.Humanoid.Died:Connect(function()
if gui then
gui:Destroy()
end
script.Disabled = true
end)
userInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.F then
if gui then
gui.Parent:Sit(character.Humanoid)
elseif currentSeat then
currentSeat:Sit(nil)
wait()
character.HumanoidRootPart.CFrame = character.HumanoidRootPart.CFrame + Vector3.new(0, 0, 5)
end
end
end)
character.Humanoid.Seated:Connect(function(active, currentSeatPart)
if active then
currentSeat = currentSeatPart
else
currentSeat = nil
end
end)
local toggleGui = function(toggle)
if gui then
gui:Destroy()
gui = nil
end
if toggle == true then
gui = carEnterGui:Clone()
gui.Parent = closestVehicle:FindFirstChildWhichIsA("VehicleSeat")
end
end
runService.RenderStepped:Connect(function()
local maxDistance = 10
local newClosestVehicle
for _, vehicle in pairs(workspace.Vehicles:GetChildren()) do
if (vehicle.DriveSeat.Position - character.HumanoidRootPart.Position).magnitude <= maxDistance and not vehicle.DriveSeat.Occupant then
maxDistance = (vehicle.DriveSeat.Position - character.HumanoidRootPart.Position).magnitude
newClosestVehicle = vehicle
end
end
closestVehicle = newClosestVehicle
if closestVehicle and not currentSeat then
toggleGui(true)
else
toggleGui(false)
end
end)
hierarchy: