How to tell what player left the seat?

Seats have a name ‘Seat1’ ‘Seat2’ and when a player sits in that seat, I specify what seat they are in into a table, so i can refer to it later. However when they choose to get up and leave, I have no way of knowing who actually left

-- Go through all the boards
for _, board in pairs(Connect4:GetChildren()) do
	if board.PrimaryPart then
		local Board = GenerateGame(board) -- Generate board
		
		-- Go through all seats
		for _, seat in pairs(board:GetDescendants()) do
			if seat:IsA("Seat") then
				seat.Changed:Connect(function()
					if seat.Occupant then -- Player has sat down
						local Character = seat.Occupant.Parent
						local Player = Players:GetPlayerFromCharacter(Character)
						if not Player then return end

						Board[seat.Parent.Name] = Player -- Give player seat
					else
						-- Find player who left and remove them from Seat
					end
				end
			end
		end
	end
end

You will have to keep a reference to the last occupant:

local occupant = seat.Occupant
seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if seat.Occupant then
		occupant = seat.Occupant
		-- ... your other code here
	else
		-- Check player that left seat:
		if (occupant) then
			local player = Players:GetPlayerFromCharacter(occupant.Parent)
		end
		occupant = nil
	end
end)
5 Likes

By looking at the script, wouldn’t Board[seat.Parent.Name] still be set to the player, I don’t see you setting the value to nil so couldn’t you just reference back to it?

1 Like