If then statement skips even though instance is not nil

local Result = MouseCast:RayCast()
		
		if Result.Instance == nil then
			return 
		end

		if Selected == nil then
			
			if Result.Instance.Parent:FindFirstChild("Humanoid") then
				
				print("d")
				
				Selected = Result.Instance.Parent
				CreateNewBoxUi(Selected)
				CreateGui(ReplicatedStorage.Gui.TroopGui)
				
			else
				
				Selected = Result.Instance
				CreateNewBoxUi(Selected)
				CreateGui(ReplicatedStorage.Gui.CityGui)
				
			end
function RayCast:RayCast()

	local MousePos = UIS:GetMouseLocation() - GuiService:GetGuiInset()
	local UnitRay = RayCast.UnitRay()
	local Origin = UnitRay.Origin
	local Direction = UnitRay.Direction
	local RayResult = workspace:Raycast(Origin, Direction * SettingsModule.MaxRayDistance, RayCast.Params())

	return RayResult

end

It skips the first if statement and moves to the other statement.
When I click on a character, it doesnt do anything.

Change if Selected == nil then to if Selected ~= nil then.