Detect if player is sitting

This is a script from a game teleporter and I wanted it so if a play is sitting in a seat inside the teleport model, it will teleport everyone in that seat to the specified placeID. How do I check if the player is sitting in the chair?

Script:

script.Parent.Gate.Touched:Connect(function(hit)
	if hit.Parent:findFirstChild("Humanoid") then
		if teleporting == false then
			local char = hit.Parent
			local player = game.Players:FindFirstChild(char.Name)
			local alreadyExists = false

			for i=1,#list do
				if list[i] == char.Name then
					alreadyExists = true
				end
			end

			if alreadyExists == false then
				if #list < 3 then -- Many Players have been teleported.
					table.insert(list,char.Name)
					char.PrimaryPart.CFrame = spawnTeleport.CFrame
					updateGui()
					leaveGuiEvent:FireClient(player)
				end

				player.CharacterRemoving:connect(function(character)
					removeFromList(character)
				end)
			end

		end
	end
end)
1 Like

To check if there is a person sitting in the seat, check the seat’s Occupant property. ex:

if seat.Occupant then
    local plr = game.Players:GetPlayerFromCharacter(seat.Occupant)
    if plr then
        --occupant and occupant is a player
    end
end
2 Likes

So would this work?

if seat.Occupant then
	local plr = game.Players:GetPlayerFromCharacter(seat.Occupant)
	if plr then
		if teleporting == false then
			local char = plr.Character
			local player = game.Players:FindFirstChild(char.Name)
			local alreadyExists = false

			for i=1,#list do
				if list[i] == char.Name then
					alreadyExists = true
				end
			end

			if alreadyExists == false then
				if #list < 3 then -- Many Players have been teleported.
					table.insert(list,char.Name)
					char.PrimaryPart.CFrame = spawnTeleport.CFrame
					updateGui()
					leaveGuiEvent:FireClient(player)
				end

				player.CharacterRemoving:connect(function(character)
					removeFromList(character)
				end)
			end

		end
	end
end
1 Like

Not sure why you have a whole new variable for plr called “player”. But, by the looks of it, it should work. (I would just remove the variable “player” and replace all references to it as “plr”)

I tried changing it all to what you said but it didn’t seem to work.

To check if a player is seated you can use the seated event, for example:
Humanoid.Seated:Connect(function()
This should be placed in a LocalScript, so you can get the LocalPlayer
Here an article explaining more.

3 Likes

That makes sense. A little problem is that I want to get everyone that is sitting in seats that are located in a specific model to be teleported all at once. So I’m not sure how I can do that.

1 Like

I would use
for i,v in pairs
and search through the model for the seats

if you want to know if a player is sitting you just do game.Players:FinadFisrtChild(player).character.Humanoid.Sit if it is true, the players is sitting

I’m trying to find which teleport seat the player is sitting at not if they’re sitting or not

Use a for i,v loop with GetDescendants() on the entire workspace. Use an if statement to check if the object is a seat, if it is, check if the Occupant property value is the specific player you are looking for. If you don’t know how to do this lmk and I can provide a code example.

HumanoidStateType you can do this

theres a function named Seating

local playersitting = false
local seat = script.parent.seat --you can change this to your seat
--detect if players sits in seat
seat.ChildAdded:Connect(function(child)
     if child.Name == "SeatWeld" then
          local character = child.Part1.Parent
          local player = game.Players.GetPlayerFromCharacter(character)
          playersitting = true
end)
--detect if players gets up from seat
seat.ChildRemoved:Connect(function(child)
     if child.Name == "SeatWeld" then
          local character = child.Part1.Parent
          local player = game.Players.GetPlayerFromCharacter(character)
          playersitting = false
end)
2 Likes

seat.Occupant is always the humanoid of a char, not the char itself. instead use:

if seat.Occupant then
	local plr = game.Players:GetPlayerFromCharacter(seat.Occupant.Parent)
	if plr then
		if teleporting == false then
			local char = plr.Character
			local player = game.Players:FindFirstChild(char.Name)
			local alreadyExists = false

			for i=1,#list do
				if list[i] == char.Name then
					alreadyExists = true
				end
			end

			if alreadyExists == false then
				if #list < 3 then -- Many Players have been teleported.
					table.insert(list,char.Name)
					char.PrimaryPart.CFrame = spawnTeleport.CFrame
					updateGui()
					leaveGuiEvent:FireClient(player)
				end

				player.CharacterRemoving:connect(function(character)
					removeFromList(character)
				end)
			end

		end
	end