Press E to enter in normal seat

Sup people basically what i want is to make like jailbreak when you near a seat it appears a button to press it and i have one done but it works just for vehicleseat, i tried changing things on it but it stopped working, if someone have any ideas how to make it, i would be very appreciated!

image

i have a folder on workspace called vehicles so every vehicle i add it works automatically

heres the script:

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, 12, 2)
		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)
3 Likes

Okay I don’t know if it’s just me but you seem to be overcomplicating the issue

Can’t you just use a ProximityPrompt instead on the Server, detect when it activates to make the player sit, and detect when the player gets out the prompt will be visible again?

3 Likes

i tried using it but to gets out it doesnt work

Try using roblox’s new proximityprompt feature as its very efficient and way easier to use. If you’re a beginner i’d definitely suggest it

edit: looks like Jackscarlett said that already

I forgot to reply back whoops.mp3

It is possible to get the current Player using this function, there’s no need to call it from a LocalScript:

Put this script as a ServerScript where your Part is supposed to be when sitting:

local Prompt = script.Parent.ProximityPrompt
local Seat = script.Parent.VehicleSeat
local canEnterGui = game.ReplicatedStorage:WaitForChild("canEnterGui")
local closestVehicle, gui, currentSeat

Prompt.Triggered:Connect(function(Player)
    local PlayerGui = Player.PlayerGui
    local Character = Player.Character
    
    Character.Humanoid.Died:Connect(function()
       	if gui then
    		gui:Destroy()
     	end
	
    	Prompt.Enabled = true
    end)

    if gui then
        Seat:Sit(Character.Humanoid)
    elseif currentSeat then
        Seat:Sit(nil)
        wait()
        Character.HumanoidRootPart.CFrame += Vector3.new(0, 12, 2)
    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
end)

wait so does this work on normal seats too?

image

i made another script with proximity prompt but it doesnt work to exit the vehicle

local Seat = script.Parent
local ProxyPromote = script.Parent:FindFirstChild("ProximityPrompt") or Instance.new("ProximityPrompt",Seat)

Seat.Disabled = true

function Sit(Hum)
	Hum.Parent.HumanoidRootPart.Position = Seat.Position
	Seat:Sit(Hum)
end

Seat.Changed:Connect(function(Property)
	if Property == "Occupant" then
		local Humanoid = Seat.Occupant
		
		if Humanoid then
			local plr = game.Players:GetPlayerFromCharacter(Humanoid.Parent)
			ProxyPromote.Enabled = false
		else
			ProxyPromote.Enabled = true
		end
	end
end)

ProxyPromote.Triggered:Connect(function(plr)
	if plr and ProxyPromote.Enabled then
		Sit(plr.Character.Humanoid)
	end
end)

What do you mean by this? Like the player can’t get out of the seat?

yes like if you press e again you cant exit

I believe what you’d need to do is implement a check to remove the player from the seat maybe?

local Seat = script.Parent
local ProxyPromote = script.Parent:FindFirstChild("ProximityPrompt") or Instance.new("ProximityPrompt",Seat)

Seat.Disabled = true

function Sit(Hum)
	Hum.Parent.HumanoidRootPart.Position = Seat.Position
	Seat:Sit(Hum)
end

Seat.Changed:Connect(function(Property)
	if Property == "Occupant" then
		local Humanoid = Seat.Occupant
		
		if Humanoid then
			local plr = game.Players:GetPlayerFromCharacter(Humanoid.Parent)
			ProxyPromote.ActionText = "Stop Car"
		else
            Humanoid.Sit = false
			ProxyPromote.ActionText = "Drive Car"
		end
	end
end)

ProxyPromote.Triggered:Connect(function(plr)
	if plr and ProxyPromote.ActionText == "Drive Car" then
		Sit(plr.Character.Humanoid)
	end
end)

This might come off as a nitpick, but is RenderStepped necessary for the “loop”? Does that block of code need to run before every frame is rendered?

If not, I believe Heartbeat or Stepped could be used instead, as RenderStepped can cause performance issues if misused.

i dont understand too much about renderstepped but if you think so

what ideas you have that i could use like teleport the player to the side of the vehicle

Considering that, I would strongly recommend you to check the hyperlinks in my post if you want to know how RenderStepped and other RunService events work. They should lead you to the Roblox API Reference Manual which contains information about them (and many other things aswell).

how i could modify the code so i can get out the car

You could change the ActionText property of the ProximityPrompt, then check if the player is still sitting to remove them from the seat? You’d need to use the PrimaryPart’s RightVector if you want to teleport the player to the side

i can use humanoidrootpart.cframe to move but how do i make like if you arent in a seat then you press f then you enter car and when you press f again you leave the seat (do the cframe thing)

Just

local Prompt = script.Parent.ProximityPrompt
local Seat = script.Parent

Prompt.Triggered:Connect(function(Player)
	local Occupant = Seat.Occupant
	local Character = Player.Character
	
	print(Player.Name)
	
	if not Occupant then
		Seat:Sit(Character.Humanoid)
	else
		Character.Humanoid.Sit = false
		wait(0.5)
		Character.HumanoidRootPart.Position = Seat.Position + Vector3.new(5, 0, 0)
	end
end)

Do something like this I guess

Also for some reason the delay when changing the property’s Humanoid has a delay so I had to implement a wait

the car bugs out and flies when i do this

I also have another question, why can’t you just jump out of the seat if you want the Player’s Character to get out of the car?

Also w h a t h o w