How Do I Detect when a player sits in a seat, and make them stay in the seat?

The question is pretty easy to understand.
In a game, I have a seat and nothing will happen until they sit in it. But if they want to hop off the seat, they cannot, because they will be stuck.

I need to know how to detect when the player sits, and how to keep them in the seat.

Thanks,
Harry.

14 Likes

You can just set their jumppower to 0 when they touch the part that is the seat

6 Likes

There are multiple ways to check if a humanoid is sitting:

  1. You can check if the Sit property of humanoid is true.
  2. You can attach a Seated event on the humanoid.
  3. On the seat, you can check if the Occupant property is nil. If it isn’t nil, then someone is sitting there.

As for not making them jump out, do what the 1st reply (@DarkDanny04 ) said.

8 Likes

Put a server script in StarterCharacterScripts

local hum = script.Parent.Humanoid

hum.Sit:GetPropertyChangedSignal("Value"):Connect(function()
    if hum.Sit.Value == true then
        hum.JumpPower = 0
    end
end)
5 Likes

Hey, the script seems to not work :confused:
image

2 Likes

Ah, my bad, do this:

local hum = script.Parent.Humanoid

hum:GetPropertyChangedSignal("Sit"):Connect(function()
    if hum.Sit.Value == true then
        hum.JumpPower = 0
    end
end)
4 Likes

You can do this a couple of ways, by checking the property Occupant of the seat itself, check the SeatPart of the players Character, or use @OIogist way.

Seat Script Example:

local Seat =  script.Parent -- Path to seat

Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if Seat.Occupant ~= nil then
		local Char = Seat.Occupant.Parent
		Char["HumanoidRootPart"].Anchored = true
        -- Can also set their jump power if wanting, your preference
	end
end)
53 Likes

You should use “hum.Sit == true” not “hum.Sit.Value == true”

local hum = script.Parent.Humanoid

hum:GetPropertyChangedSignal("Sit"):Connect(function()
    if hum.Sit == true then
        hum.JumpPower = 0
    end
end)

EDIT: I realized that it will detect if u seat but it doesn’t specify a seat so I will add that code soon also ignore the ‘’'hum.JumpPower = 0", it’s not needed

9 Likes

hello, I need some help with something.
I want to make it so that if a humanoid is in a seat, a GUI appears on the screen and if they leave the seat it disappears.

here is my code:

local DriverSeat = script.Parent

local function Seated()
if DriverSeat.Occupant ~= nil then

end

end
DriverSeat:GetPropertyChangedSignal(“Occupant”):Connect(Seated)

1 Like

hello, so first to make the gui appear you need to add the code inside the if statement, i cant write the code because i don’t know if the gui is a screen gui or frame…>
second you need to add another if hat detects if the driverseat.occupant == nil(player left) so you can remove the gui when the player leaves here’s how the code will be:

local DriverSeat = script.Parent

local function Seated()
if DriverSeat.Occupant ~= nil then
      -- code to make the gui appear
end
if DriverSeat.Occupant == nil then
      --code to make the gui disappear
end

end
DriverSeat:GetPropertyChangedSignal(“Occupant”):Connect(Seated)

now if the code doesn’t work resend the script with its location in the explorer because i have a bug in mind but it might work

4 Likes

Okay, thanks for the help, but what does “~=“ mean?

for example, it checks if a variable is NOT equal to an other variable

2 Likes

just as wytze2 said it means not equal to

olay thanks for the meaning i never knew what it meant before

To add on to this, I find it works best if I do anything related to sitting in one script under ServerScriptService, like so:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:FindFirstChild("Humanoid")
		
		humanoid:GetPropertyChangedSignal("Sit"):Connect(function()
			if humanoid.Sit then
				player:SetAttribute("IsSitting", true)
				
			else
				player:SetAttribute("IsSitting", false)
				
			end
			
		end)
		
	end)
	
end)

This easily finds the player, character, and humanoid. The reason I use an attribute and not a local boolean variable is so that I can easily access it from other scripts, if needed, using :GetAttributeChangedSignal("IsSitting").

1 Like

Hey man

Im having trouble with my code.

I want the gui to only appear on the player who is seated
Can you help?

Here is my code:

local Passpart = game.Workspace.PassPart
local prox = Passpart.ProximityPrompt

prox.Triggered:Connect(function(plr)
		if plr.Backpack:FindFirstChild("Pasaport") then
			
			print("Pasaport Bulundu!")

			local Seat = game.Workspace.SMSEAT

			local function Seated()
				if Seat.Occupant ~= nil then
					
					game.StarterGui["Pasaport Gui"].Passaport.Visible = true

				end
				if Seat.Occupant == nil then
					
					game.StarterGui["Pasaport Gui"].Passaport.Visible = false
					
				end

			end
			Seat:GetPropertyChangedSignal("Occupant"):Connect(Seated)





		else


			print("Pasaport Bulunamadı")


		end
		
	end)

You have to replace game.Startergui
With
plr.PlayerGui