How would I detect who is sitting in a seat and what seat

Hi, so I am wondering how I could detect somebody sitting in a seat, who is it sitting in the seat and what seat they are sitting in. This is for a suggestion somebody gave me for my train game where you drive trains around, to have it so a specific train you could get with a gamepass had it so when you sat in the seat, the player’s face would show up on the front of the train . Basically the face ID of the player = the ID of the decal on a part.
I am not a very good scripter and I am stuck on how I would check if the player if sitting in the specific seat, and how to get the players face ID. I know how I could do the rest though. Any help would be appreciated, thanks

2 Likes

There is an event we can make for this for the seat from the dev reference API and there even is code for detecting who is sitting on that seat.

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)

Getting the face is a different matter though but it’s possible as you can get the character and the player of whoever is sitting on the seat. The character will probably have the face decal in the humanoid description or within the character model somewhere. I believe you can find your way to work towards that goal by yourself but feel free to ask further questions if needed.

1 Like

Going off of what @dthecoolest said, I put down a seat in the workspace and put in this script.

Demo and explanation below.

local seat = script.Parent
local part = workspace.Part
seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if seat.Occupant then
	local occupant = seat.Occupant
	local char = occupant.Parent
	local face = char.Head.face
	if face then
			local id = face.Texture
			local color = face.Color3
			part.Decal.Texture = id
			part.Decal.Color3 = Color3.fromRGB(color)
		end
	else
		part.Decal.Texture = ""
		part.Decal.Color3 = Color3.fromRGB(255,255,255)
	end
end)

Essentially, it gets the occupant’s face ID and plasters it on the part beside it. It also removes the decal when they die or they get off of the chair.

Demo:
https://imgur.com/a/oyL8yWD

Hope this helps!

2 Likes