How do I get the player after they have gotten out of a seat?

Hey, I have a question.

So lets say I have code like this:

local PlayerSitting = nil

local function SitPlayerInSeat(Seats)
Seats:GetPropertyChangedSignal("Occupant"):Connect(function()	
		PlayerSitting = Seats.Occupant
		
		if PlayerSitting then
			print('ok')
		else
			PlayerSitting.CFrame == --place, BUT PLAYERSITTING IS NIL!!!-- 
	end
end)
end


for _, Seat in pairs(CollectionService:GetTagged("Seats")) do 
	SitPlayerInSeat(Seat)
end

How would I achieve getting the player after they already sat down? (This is in a server script).

(Sorry for ugly formatting)

If you don’t want the player to get out of the seat, then have an event that fires when they sit, and set their jumppower to 0.

I want to detect which player gets out of the seat.

Hi, sorry for the late response.

In the DevForum I found this:

    local Players = game:GetService("Players")
    
    local seat = Instance.new("Seat")
    seat.Anchored = true 
    seat.Position = Vector3.new(0, 1, 0)
    seat.Parent = workspace 
    
    local currentPlayer = nil
    
    seat:GetPropertyChangedSignal("Occupant"):Connect(function()
    	local humanoid = seat.Occupant 
    	if humanoid then 
    		local character = humanoid.Parent
    		local player = Players:GetPlayerFromCharacter(character)
    		if player then 
    			print(player.Name.." has sat down")
    			currentPlayer = player
    			return
    		end	
    	end
    	if currentPlayer then 
    		print(currentPlayer.Name.." has got up")
    		currentPlayer = nil
    	end
    end)

Source: Seat | Roblox Creator Documentation

Maybe this can help you, and if you need help adapting the code to your setup, feel free to reply.

Grayseon

I do not think this would work.

Did it give you an error when you tested it?

This is fairly easy to sort out.

When a character sits down, the character’s root and the seat get welded together. This results in there being a weld parented to the seat.

With this information, we can now check for when a child is removed from the seat - which happens when the character jumps out of the seat.

local Seat = script.Parent

Seat.ChildRemoved:Connect(function(Child)
	if Child:IsA("Weld") then
		print(Child.Part1.Parent)
	end
end)

In the code snippet above, Child.Part1 is the HumanoidRootPart. The parent would then refer to the player’s character.

Moving on from this, we can get the Player instance by calling the Players:GetPlayerFromCharacter() function.

local Seat = script.Parent

Seat.ChildRemoved:Connect(function(Child)
	if Child:IsA("Weld") then
		local Player = game.Players:GetPlayerFromCharacter(Child.Part1.Parent)
		print(Player)
	end
end)
2 Likes

I already figured it out:

local seat = -- your seat
local player = nil

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
  if seat.Occupant then 
    local humanoid = seat.Occupant
   player = game.Players:GetPlayerFromCharacter(humanoid.Parent) 
  else
     
  end
end)
2 Likes