Unable to cast value to Object

I’m trying to double check if player leaving the part is the same player that entered the part in first place and im trying check that with UserId and it gives error for one that left the part and if i try to do it like this:

local uh = players:GetPlayerFromCharacter(player)

then it says player is a nil.
(btw “unable to cast value to object” error is at line 139)

It’s hard to understand what your code is doing with the variable names and you need to show what the player variable is.

I don’t know what to call variables i just named them to test. those variables are used to get UserId of player that left the part and entered the part so i can compare them and according to that it changes values. here is larger version of the code:

while true do
	local leftParts = workspace:GetPartsInPart(LeftPart)
	local rightParts = workspace:GetPartsInPart(RightPart)

	local leftPlayers = {}
	local rightPlayers = {}

	for _, part in ipairs(leftParts) do
		local player = players:GetPlayerFromCharacter(part.Parent)
		if player then
			leftPlayers[player] = true
		end
	end

	for _, part in ipairs(rightParts) do
		local player = players:GetPlayerFromCharacter(part.Parent)
		if player then
			rightPlayers[player] = true
		end
	end

	for player, _ in pairs(leftPlayers) do
		if not playersInPartLastIteration[player] then
			print(player.Name .. ' entered LeftPart')
			LeftTextLabel.Text = "1/1"
		end
	end

	for player, _ in pairs(rightPlayers) do
		if not playersInPartLastIteration[player] then
			print(player.Name .. ' entered RightPart')
			RightTextLabel.Text = "1/1"
		end
	end

	for player, _ in pairs(playersInPartLastIteration) do
		if leftPlayers[player] ~= nil then
			local eh = leftPlayers[player]
			local uh = players:GetPlayerFromCharacter(eh)
			local oh = players:GetPlayerFromCharacter(HitPlayer)
			print(oh)
			print(uh)
			if uh.UserId ==  oh.UserId and not leftPlayers[player] then
				Table[1] = nil
				sum = nil
				LeftTextLabel.Text = "0/1"
			else
				print("Third party interacted")
			end
		elseif rightPlayers[player] ~= nil then
			if not rightPlayers[player] and rightPlayers[player.UserId] == HitPlayer_.UserId then
				Table[2] = nil
				sum_ = nil
				RightTextLabel.Text = "0/1"
			else
				print("Third party interacted")
			end
		end
	end

	playersInPartLastIteration = {}
	for player, _ in pairs(leftPlayers) do
		playersInPartLastIteration[player] = true
	end
	for player, _ in pairs(rightPlayers) do
		playersInPartLastIteration[player] = true
	end

	task.wait()
end